setup.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /* grub-setup.c - make GRUB usable */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/emu/misc.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/device.h>
  24. #include <grub/disk.h>
  25. #include <grub/file.h>
  26. #include <grub/fs.h>
  27. #include <grub/partition.h>
  28. #include <grub/env.h>
  29. #include <grub/emu/hostdisk.h>
  30. #include <grub/term.h>
  31. #include <grub/i18n.h>
  32. #ifdef GRUB_SETUP_SPARC64
  33. #include <grub/util/ofpath.h>
  34. #include <grub/sparc64/ieee1275/boot.h>
  35. #include <grub/sparc64/ieee1275/kernel.h>
  36. #else
  37. #include <grub/i386/pc/boot.h>
  38. #include <grub/i386/pc/kernel.h>
  39. #endif
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <dirent.h>
  47. #include <assert.h>
  48. #include <grub/emu/getroot.h>
  49. #include "progname.h"
  50. #include <grub/reed_solomon.h>
  51. #include <grub/msdos_partition.h>
  52. #include <grub/crypto.h>
  53. #include <grub/util/install.h>
  54. #include <grub/emu/hostfile.h>
  55. #include <errno.h>
  56. /* On SPARC this program fills in various fields inside of the 'boot' and 'core'
  57. * image files.
  58. *
  59. * The 'boot' image needs to know the OBP path name of the root
  60. * device. It also needs to know the initial block number of
  61. * 'core' (which is 'diskboot' concatenated with 'kernel' and
  62. * all the modules, this is created by grub-mkimage). This resulting
  63. * 'boot' image is 512 bytes in size and is placed in the second block
  64. * of a partition.
  65. *
  66. * The initial 'diskboot' block acts as a loader for the actual GRUB
  67. * kernel. It contains the loading code and then a block list.
  68. *
  69. * The block list of 'core' starts at the end of the 'diskboot' image
  70. * and works it's way backwards towards the end of the code of 'diskboot'.
  71. *
  72. * We patch up the images with the necessary values and write out the
  73. * result.
  74. */
  75. #ifdef GRUB_SETUP_SPARC64
  76. #define grub_target_to_host16(x) grub_be_to_cpu16(x)
  77. #define grub_target_to_host32(x) grub_be_to_cpu32(x)
  78. #define grub_target_to_host64(x) grub_be_to_cpu64(x)
  79. #define grub_host_to_target16(x) grub_cpu_to_be16(x)
  80. #define grub_host_to_target32(x) grub_cpu_to_be32(x)
  81. #define grub_host_to_target64(x) grub_cpu_to_be64(x)
  82. #elif defined (GRUB_SETUP_BIOS)
  83. #define grub_target_to_host16(x) grub_le_to_cpu16(x)
  84. #define grub_target_to_host32(x) grub_le_to_cpu32(x)
  85. #define grub_target_to_host64(x) grub_le_to_cpu64(x)
  86. #define grub_host_to_target16(x) grub_cpu_to_le16(x)
  87. #define grub_host_to_target32(x) grub_cpu_to_le32(x)
  88. #define grub_host_to_target64(x) grub_cpu_to_le64(x)
  89. #else
  90. #error Complete this
  91. #endif
  92. static void
  93. write_rootdev (grub_device_t root_dev,
  94. char *boot_img, grub_uint64_t first_sector)
  95. {
  96. #ifdef GRUB_SETUP_BIOS
  97. {
  98. grub_uint8_t *boot_drive;
  99. void *kernel_sector;
  100. boot_drive = (grub_uint8_t *) (boot_img + GRUB_BOOT_MACHINE_BOOT_DRIVE);
  101. kernel_sector = (boot_img + GRUB_BOOT_MACHINE_KERNEL_SECTOR);
  102. /* FIXME: can this be skipped? */
  103. *boot_drive = 0xFF;
  104. grub_set_unaligned64 (kernel_sector, grub_cpu_to_le64 (first_sector));
  105. }
  106. #endif
  107. #ifdef GRUB_SETUP_SPARC64
  108. {
  109. void *kernel_byte;
  110. kernel_byte = (boot_img + GRUB_BOOT_AOUT_HEADER_SIZE
  111. + GRUB_BOOT_MACHINE_KERNEL_BYTE);
  112. grub_set_unaligned64 (kernel_byte,
  113. grub_cpu_to_be64 (first_sector << GRUB_DISK_SECTOR_BITS));
  114. }
  115. #endif
  116. }
  117. #ifdef GRUB_SETUP_SPARC64
  118. #define BOOT_SECTOR 1
  119. #else
  120. #define BOOT_SECTOR 0
  121. #endif
  122. /* Helper for setup. */
  123. struct blocklists
  124. {
  125. struct grub_boot_blocklist *first_block, *block;
  126. #ifdef GRUB_SETUP_BIOS
  127. grub_uint16_t current_segment;
  128. #endif
  129. #ifdef GRUB_SETUP_SPARC64
  130. grub_uint64_t gpt_offset;
  131. #endif
  132. grub_uint16_t last_length;
  133. grub_disk_addr_t first_sector;
  134. };
  135. /* Helper for setup. */
  136. static void
  137. save_blocklists (grub_disk_addr_t sector, unsigned offset, unsigned length,
  138. void *data)
  139. {
  140. struct blocklists *bl = data;
  141. struct grub_boot_blocklist *prev = bl->block + 1;
  142. grub_uint64_t seclen;
  143. #ifdef GRUB_SETUP_SPARC64
  144. sector -= bl->gpt_offset;
  145. #endif
  146. grub_util_info ("saving <%" GRUB_HOST_PRIuLONG_LONG ",%u,%u>",
  147. (unsigned long long) sector, offset, length);
  148. if (bl->first_sector == (grub_disk_addr_t) -1)
  149. {
  150. if (offset != 0 || length < GRUB_DISK_SECTOR_SIZE)
  151. grub_util_error ("%s", _("the first sector of the core file is not sector-aligned"));
  152. bl->first_sector = sector;
  153. sector++;
  154. length -= GRUB_DISK_SECTOR_SIZE;
  155. if (!length)
  156. return;
  157. }
  158. if (offset != 0 || bl->last_length != 0)
  159. grub_util_error ("%s", _("non-sector-aligned data is found in the core file"));
  160. seclen = (length + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS;
  161. if (bl->block != bl->first_block
  162. && (grub_target_to_host64 (prev->start)
  163. + grub_target_to_host16 (prev->len)) == sector)
  164. {
  165. grub_uint16_t t = grub_target_to_host16 (prev->len);
  166. t += seclen;
  167. prev->len = grub_host_to_target16 (t);
  168. }
  169. else
  170. {
  171. bl->block->start = grub_host_to_target64 (sector);
  172. bl->block->len = grub_host_to_target16 (seclen);
  173. #ifdef GRUB_SETUP_BIOS
  174. bl->block->segment = grub_host_to_target16 (bl->current_segment);
  175. #endif
  176. bl->block--;
  177. if (bl->block->len)
  178. grub_util_error ("%s", _("the sectors of the core file are too fragmented"));
  179. }
  180. bl->last_length = length & (GRUB_DISK_SECTOR_SIZE - 1);
  181. #ifdef GRUB_SETUP_BIOS
  182. bl->current_segment += seclen << (GRUB_DISK_SECTOR_BITS - 4);
  183. #endif
  184. }
  185. /* Context for setup/identify_partmap. */
  186. struct identify_partmap_ctx
  187. {
  188. grub_partition_map_t dest_partmap;
  189. grub_partition_t container;
  190. int multiple_partmaps;
  191. };
  192. /* Helper for setup.
  193. Unlike root_dev, with dest_dev we're interested in the partition map even
  194. if dest_dev itself is a whole disk. */
  195. static int
  196. identify_partmap (grub_disk_t disk __attribute__ ((unused)),
  197. const grub_partition_t p, void *data)
  198. {
  199. struct identify_partmap_ctx *ctx = data;
  200. if (p->parent != ctx->container)
  201. return 0;
  202. /* NetBSD and OpenBSD subpartitions have metadata inside a partition,
  203. so they are safe to ignore.
  204. */
  205. if (grub_strcmp (p->partmap->name, "netbsd") == 0
  206. || grub_strcmp (p->partmap->name, "openbsd") == 0)
  207. return 0;
  208. if (ctx->dest_partmap == NULL)
  209. {
  210. ctx->dest_partmap = p->partmap;
  211. return 0;
  212. }
  213. if (ctx->dest_partmap == p->partmap)
  214. return 0;
  215. ctx->multiple_partmaps = 1;
  216. return 1;
  217. }
  218. #ifdef GRUB_SETUP_BIOS
  219. #define SETUP grub_util_bios_setup
  220. #elif GRUB_SETUP_SPARC64
  221. #define SETUP grub_util_sparc_setup
  222. #else
  223. #error "Shouldn't happen"
  224. #endif
  225. void
  226. SETUP (const char *dir,
  227. const char *boot_file, const char *core_file,
  228. const char *dest, int force,
  229. int fs_probe, int allow_floppy,
  230. int add_rs_codes __attribute__ ((unused))) /* unused on sparc64 */
  231. {
  232. char *core_path;
  233. char *boot_img, *core_img, *boot_path;
  234. char *root = 0;
  235. size_t boot_size, core_size;
  236. grub_uint16_t core_sectors;
  237. grub_device_t root_dev = 0, dest_dev, core_dev;
  238. grub_util_fd_t fp;
  239. struct blocklists bl;
  240. bl.first_sector = (grub_disk_addr_t) -1;
  241. #ifdef GRUB_SETUP_BIOS
  242. bl.current_segment =
  243. GRUB_BOOT_I386_PC_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
  244. #endif
  245. bl.last_length = 0;
  246. /* Read the boot image by the OS service. */
  247. boot_path = grub_util_get_path (dir, boot_file);
  248. boot_size = grub_util_get_image_size (boot_path);
  249. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  250. grub_util_error (_("the size of `%s' is not %u"),
  251. boot_path, GRUB_DISK_SECTOR_SIZE);
  252. boot_img = grub_util_read_image (boot_path);
  253. free (boot_path);
  254. core_path = grub_util_get_path (dir, core_file);
  255. core_size = grub_util_get_image_size (core_path);
  256. core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
  257. >> GRUB_DISK_SECTOR_BITS);
  258. if (core_size < GRUB_DISK_SECTOR_SIZE)
  259. grub_util_error (_("the size of `%s' is too small"), core_path);
  260. #ifdef GRUB_SETUP_BIOS
  261. if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
  262. grub_util_error (_("the size of `%s' is too large"), core_path);
  263. #endif
  264. core_img = grub_util_read_image (core_path);
  265. /* Have FIRST_BLOCK to point to the first blocklist. */
  266. bl.first_block = (struct grub_boot_blocklist *) (core_img
  267. + GRUB_DISK_SECTOR_SIZE
  268. - sizeof (*bl.block));
  269. grub_util_info ("Opening dest `%s'", dest);
  270. dest_dev = grub_device_open (dest);
  271. if (! dest_dev)
  272. grub_util_error ("%s", grub_errmsg);
  273. core_dev = dest_dev;
  274. {
  275. char **root_devices = grub_guess_root_devices (dir);
  276. char **cur;
  277. int found = 0;
  278. if (!root_devices)
  279. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"), dir);
  280. for (cur = root_devices; *cur; cur++)
  281. {
  282. char *drive;
  283. grub_device_t try_dev;
  284. drive = grub_util_get_grub_dev (*cur);
  285. if (!drive)
  286. continue;
  287. try_dev = grub_device_open (drive);
  288. if (! try_dev)
  289. {
  290. free (drive);
  291. continue;
  292. }
  293. if (!found && try_dev->disk->id == dest_dev->disk->id
  294. && try_dev->disk->dev->id == dest_dev->disk->dev->id)
  295. {
  296. if (root_dev)
  297. grub_device_close (root_dev);
  298. free (root);
  299. root_dev = try_dev;
  300. root = drive;
  301. found = 1;
  302. continue;
  303. }
  304. if (!root_dev)
  305. {
  306. root_dev = try_dev;
  307. root = drive;
  308. continue;
  309. }
  310. grub_device_close (try_dev);
  311. free (drive);
  312. }
  313. if (!root_dev)
  314. {
  315. grub_util_error ("guessing the root device failed, because of `%s'",
  316. grub_errmsg);
  317. }
  318. grub_util_info ("guessed root_dev `%s' from "
  319. "dir `%s'", root_dev->disk->name, dir);
  320. for (cur = root_devices; *cur; cur++)
  321. free (*cur);
  322. free (root_devices);
  323. }
  324. grub_util_info ("setting the root device to `%s'", root);
  325. if (grub_env_set ("root", root) != GRUB_ERR_NONE)
  326. grub_util_error ("%s", grub_errmsg);
  327. {
  328. #ifdef GRUB_SETUP_BIOS
  329. char *tmp_img;
  330. grub_uint8_t *boot_drive_check;
  331. /* Read the original sector from the disk. */
  332. tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
  333. if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
  334. grub_util_error ("%s", grub_errmsg);
  335. boot_drive_check = (grub_uint8_t *) (boot_img
  336. + GRUB_BOOT_MACHINE_DRIVE_CHECK);
  337. /* Copy the possible DOS BPB. */
  338. memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
  339. tmp_img + GRUB_BOOT_MACHINE_BPB_START,
  340. GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
  341. /* If DEST_DRIVE is a hard disk, enable the workaround, which is
  342. for buggy BIOSes which don't pass boot drive correctly. Instead,
  343. they pass 0x00 or 0x01 even when booted from 0x80. */
  344. if (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk))
  345. {
  346. /* Replace the jmp (2 bytes) with double nop's. */
  347. boot_drive_check[0] = 0x90;
  348. boot_drive_check[1] = 0x90;
  349. }
  350. #endif
  351. struct identify_partmap_ctx ctx = {
  352. .dest_partmap = NULL,
  353. .container = dest_dev->disk->partition,
  354. .multiple_partmaps = 0
  355. };
  356. int is_ldm;
  357. grub_err_t err;
  358. grub_disk_addr_t *sectors;
  359. int i;
  360. grub_fs_t fs;
  361. unsigned int nsec, maxsec;
  362. grub_partition_iterate (dest_dev->disk, identify_partmap, &ctx);
  363. #ifdef GRUB_SETUP_BIOS
  364. /* Copy the partition table. */
  365. if (ctx.dest_partmap ||
  366. (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk)))
  367. memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  368. tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  369. GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
  370. free (tmp_img);
  371. #endif
  372. if (ctx.container
  373. && grub_strcmp (ctx.container->partmap->name, "msdos") == 0
  374. && ctx.dest_partmap
  375. && (ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_NETBSD
  376. || ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_OPENBSD))
  377. {
  378. grub_util_warn ("%s", _("Attempting to install GRUB to a disk with multiple partition labels or both partition label and filesystem. This is not supported yet."));
  379. goto unable_to_embed;
  380. }
  381. fs = grub_fs_probe (dest_dev);
  382. if (!fs)
  383. grub_errno = GRUB_ERR_NONE;
  384. is_ldm = grub_util_is_ldm (dest_dev->disk);
  385. if (fs_probe)
  386. {
  387. if (!fs && !ctx.dest_partmap)
  388. grub_util_error (_("unable to identify a filesystem in %s; safety check can't be performed"),
  389. dest_dev->disk->name);
  390. if (fs && !fs->reserved_first_sector)
  391. /* TRANSLATORS: Filesystem may reserve the space just GRUB isn't sure about it. */
  392. grub_util_error (_("%s appears to contain a %s filesystem which isn't known to "
  393. "reserve space for DOS-style boot. Installing GRUB there could "
  394. "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
  395. "by grub-setup (--skip-fs-probe disables this "
  396. "check, use at your own risk)"), dest_dev->disk->name, fs->name);
  397. if (ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
  398. && strcmp (ctx.dest_partmap->name, "gpt") != 0
  399. && strcmp (ctx.dest_partmap->name, "bsd") != 0
  400. && strcmp (ctx.dest_partmap->name, "netbsd") != 0
  401. && strcmp (ctx.dest_partmap->name, "openbsd") != 0
  402. && strcmp (ctx.dest_partmap->name, "sunpc") != 0)
  403. /* TRANSLATORS: Partition map may reserve the space just GRUB isn't sure about it. */
  404. grub_util_error (_("%s appears to contain a %s partition map which isn't known to "
  405. "reserve space for DOS-style boot. Installing GRUB there could "
  406. "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
  407. "by grub-setup (--skip-fs-probe disables this "
  408. "check, use at your own risk)"), dest_dev->disk->name, ctx.dest_partmap->name);
  409. if (is_ldm && ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
  410. && strcmp (ctx.dest_partmap->name, "gpt") != 0)
  411. grub_util_error (_("%s appears to contain a %s partition map and "
  412. "LDM which isn't known to be a safe combination."
  413. " Installing GRUB there could "
  414. "result in FILESYSTEM DESTRUCTION if valuable data"
  415. " is overwritten "
  416. "by grub-setup (--skip-fs-probe disables this "
  417. "check, use at your own risk)"),
  418. dest_dev->disk->name, ctx.dest_partmap->name);
  419. }
  420. if (! ctx.dest_partmap && ! fs && !is_ldm)
  421. {
  422. grub_util_warn ("%s", _("Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea."));
  423. goto unable_to_embed;
  424. }
  425. if (ctx.multiple_partmaps || (ctx.dest_partmap && fs) || (is_ldm && fs))
  426. {
  427. grub_util_warn ("%s", _("Attempting to install GRUB to a disk with multiple partition labels. This is not supported yet."));
  428. goto unable_to_embed;
  429. }
  430. if (ctx.dest_partmap && !ctx.dest_partmap->embed)
  431. {
  432. grub_util_warn (_("Partition style `%s' doesn't support embedding"),
  433. ctx.dest_partmap->name);
  434. goto unable_to_embed;
  435. }
  436. if (fs && !fs->fs_embed)
  437. {
  438. grub_util_warn (_("File system `%s' doesn't support embedding"),
  439. fs->name);
  440. goto unable_to_embed;
  441. }
  442. nsec = core_sectors;
  443. if (add_rs_codes)
  444. maxsec = 2 * core_sectors;
  445. else
  446. maxsec = core_sectors;
  447. #ifdef GRUB_SETUP_BIOS
  448. if (maxsec > ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
  449. >> GRUB_DISK_SECTOR_BITS))
  450. maxsec = ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
  451. >> GRUB_DISK_SECTOR_BITS);
  452. #endif
  453. #ifdef GRUB_SETUP_SPARC64
  454. /*
  455. * On SPARC we need two extra. One is because we are combining the
  456. * core.img with the boot.img. The other is because the boot sector
  457. * starts at 1.
  458. */
  459. nsec += 2;
  460. maxsec += 2;
  461. #endif
  462. if (is_ldm)
  463. err = grub_util_ldm_embed (dest_dev->disk, &nsec, maxsec,
  464. GRUB_EMBED_PCBIOS, &sectors);
  465. else if (ctx.dest_partmap)
  466. err = ctx.dest_partmap->embed (dest_dev->disk, &nsec, maxsec,
  467. GRUB_EMBED_PCBIOS, &sectors);
  468. else
  469. err = fs->fs_embed (dest_dev, &nsec, maxsec,
  470. GRUB_EMBED_PCBIOS, &sectors);
  471. if (!err && nsec < core_sectors)
  472. {
  473. err = grub_error (GRUB_ERR_OUT_OF_RANGE,
  474. N_("Your embedding area is unusually small. "
  475. "core.img won't fit in it."));
  476. }
  477. if (err)
  478. {
  479. grub_util_warn ("%s", grub_errmsg);
  480. grub_errno = GRUB_ERR_NONE;
  481. goto unable_to_embed;
  482. }
  483. assert (nsec <= maxsec);
  484. /* Clean out the blocklists. */
  485. bl.block = bl.first_block;
  486. while (bl.block->len)
  487. {
  488. grub_memset (bl.block, 0, sizeof (*bl.block));
  489. bl.block--;
  490. if ((char *) bl.block <= core_img)
  491. grub_util_error ("%s", _("no terminator in the core image"));
  492. }
  493. bl.block = bl.first_block;
  494. for (i = 0; i < nsec; i++)
  495. save_blocklists (sectors[i] + grub_partition_get_start (ctx.container),
  496. 0, GRUB_DISK_SECTOR_SIZE, &bl);
  497. /* Make sure that the last blocklist is a terminator. */
  498. if (bl.block == bl.first_block)
  499. bl.block--;
  500. bl.block->start = 0;
  501. bl.block->len = 0;
  502. #ifdef GRUB_SETUP_BIOS
  503. bl.block->segment = 0;
  504. #endif
  505. #ifdef GRUB_SETUP_SPARC64
  506. {
  507. /*
  508. * On SPARC, the block-list entries need to be based off the beginning
  509. * of the parition, not the beginning of the disk.
  510. */
  511. struct grub_boot_blocklist *block;
  512. block = bl.first_block;
  513. while (block->len)
  514. {
  515. block->start -= bl.first_sector;
  516. block--;
  517. }
  518. }
  519. /*
  520. * Reserve space for the boot block since it can not be in the
  521. * Parition table on SPARC.
  522. */
  523. assert (bl.first_block->len > 2);
  524. bl.first_block->start += 2;
  525. bl.first_block->len -= 2;
  526. write_rootdev (root_dev, boot_img, sectors[BOOT_SECTOR + 1] - bl.first_sector);
  527. #endif
  528. #ifdef GRUB_SETUP_BIOS
  529. write_rootdev (root_dev, boot_img, bl.first_sector);
  530. #endif
  531. /* Round up to the nearest sector boundary, and zero the extra memory */
  532. core_img = xrealloc (core_img, nsec * GRUB_DISK_SECTOR_SIZE);
  533. assert (core_img && (nsec * GRUB_DISK_SECTOR_SIZE >= core_size));
  534. memset (core_img + core_size, 0, nsec * GRUB_DISK_SECTOR_SIZE - core_size);
  535. bl.first_block = (struct grub_boot_blocklist *) (core_img
  536. + GRUB_DISK_SECTOR_SIZE
  537. - sizeof (*bl.block));
  538. #if GRUB_SETUP_BIOS
  539. grub_size_t no_rs_length;
  540. no_rs_length = grub_target_to_host16
  541. (grub_get_unaligned16 (core_img
  542. + GRUB_DISK_SECTOR_SIZE
  543. + GRUB_KERNEL_I386_PC_NO_REED_SOLOMON_LENGTH));
  544. if (no_rs_length == 0xffff)
  545. grub_util_error ("%s", _("core.img version mismatch"));
  546. if (add_rs_codes)
  547. {
  548. grub_set_unaligned32 ((core_img + GRUB_DISK_SECTOR_SIZE
  549. + GRUB_KERNEL_I386_PC_REED_SOLOMON_REDUNDANCY),
  550. grub_host_to_target32 (nsec * GRUB_DISK_SECTOR_SIZE - core_size));
  551. void *tmp = xmalloc (core_size);
  552. grub_memcpy (tmp, core_img, core_size);
  553. grub_reed_solomon_add_redundancy (core_img + no_rs_length + GRUB_DISK_SECTOR_SIZE,
  554. core_size - no_rs_length - GRUB_DISK_SECTOR_SIZE,
  555. nsec * GRUB_DISK_SECTOR_SIZE
  556. - core_size);
  557. assert (grub_memcmp (tmp, core_img, core_size) == 0);
  558. free (tmp);
  559. }
  560. /* Write the core image onto the disk. */
  561. for (i = 0; i < nsec; i++)
  562. grub_disk_write (dest_dev->disk, sectors[i], 0,
  563. GRUB_DISK_SECTOR_SIZE,
  564. core_img + i * GRUB_DISK_SECTOR_SIZE);
  565. #endif
  566. #ifdef GRUB_SETUP_SPARC64
  567. {
  568. int isec = BOOT_SECTOR;
  569. /* Write the boot image onto the disk. */
  570. if (grub_disk_write (dest_dev->disk, sectors[isec++], 0,
  571. GRUB_DISK_SECTOR_SIZE, boot_img))
  572. grub_util_error ("%s", grub_errmsg);
  573. /* Write the core image onto the disk. */
  574. for (i = 0 ; isec < nsec; i++, isec++)
  575. {
  576. if (grub_disk_write (dest_dev->disk, sectors[isec], 0,
  577. GRUB_DISK_SECTOR_SIZE,
  578. core_img + i * GRUB_DISK_SECTOR_SIZE))
  579. grub_util_error ("%s", grub_errmsg);
  580. }
  581. }
  582. #endif
  583. grub_free (sectors);
  584. goto finish;
  585. }
  586. unable_to_embed:
  587. if (dest_dev->disk->dev->id != root_dev->disk->dev->id)
  588. grub_util_error ("%s", _("embedding is not possible, but this is required for "
  589. "RAID and LVM install"));
  590. {
  591. grub_fs_t fs;
  592. fs = grub_fs_probe (root_dev);
  593. if (!fs)
  594. grub_util_error (_("can't determine filesystem on %s"), root);
  595. if (!fs->blocklist_install)
  596. grub_util_error (_("filesystem `%s' doesn't support blocklists"),
  597. fs->name);
  598. }
  599. #ifdef GRUB_SETUP_BIOS
  600. if (dest_dev->disk->id != root_dev->disk->id
  601. || dest_dev->disk->dev->id != root_dev->disk->dev->id)
  602. /* TRANSLATORS: cross-disk refers to /boot being on one disk
  603. but MBR on another. */
  604. grub_util_error ("%s", _("embedding is not possible, but this is required for "
  605. "cross-disk install"));
  606. #else
  607. core_dev = root_dev;
  608. #endif
  609. grub_util_warn ("%s", _("Embedding is not possible. GRUB can only be installed in this "
  610. "setup by using blocklists. However, blocklists are UNRELIABLE and "
  611. "their use is discouraged."));
  612. if (! force)
  613. /* TRANSLATORS: Here GRUB refuses to continue with blocklist install. */
  614. grub_util_error ("%s", _("will not proceed with blocklists"));
  615. /* The core image must be put on a filesystem unfortunately. */
  616. grub_util_info ("will leave the core image on the filesystem");
  617. grub_util_biosdisk_flush (root_dev->disk);
  618. /* Clean out the blocklists. */
  619. bl.block = bl.first_block;
  620. while (bl.block->len)
  621. {
  622. bl.block->start = 0;
  623. bl.block->len = 0;
  624. #ifdef GRUB_SETUP_BIOS
  625. bl.block->segment = 0;
  626. #endif
  627. bl.block--;
  628. if ((char *) bl.block <= core_img)
  629. grub_util_error ("%s", _("no terminator in the core image"));
  630. }
  631. bl.block = bl.first_block;
  632. #ifdef GRUB_SETUP_SPARC64
  633. {
  634. grub_partition_t container = root_dev->disk->partition;
  635. bl.gpt_offset = 0;
  636. if (grub_strstr (container->partmap->name, "gpt"))
  637. bl.gpt_offset = grub_partition_get_start (container);
  638. }
  639. #endif
  640. grub_install_get_blocklist (root_dev, core_path, core_img, core_size,
  641. save_blocklists, &bl);
  642. if (bl.first_sector == (grub_disk_addr_t)-1)
  643. grub_util_error ("%s", _("can't retrieve blocklists"));
  644. #ifdef GRUB_SETUP_SPARC64
  645. {
  646. char *boot_devpath;
  647. boot_devpath = (char *) (boot_img
  648. + GRUB_BOOT_AOUT_HEADER_SIZE
  649. + GRUB_BOOT_MACHINE_BOOT_DEVPATH);
  650. if (dest_dev->disk->id != root_dev->disk->id
  651. || dest_dev->disk->dev->id != root_dev->disk->dev->id)
  652. {
  653. char *dest_ofpath;
  654. dest_ofpath
  655. = grub_util_devname_to_ofpath (grub_util_biosdisk_get_osdev (root_dev->disk));
  656. /* FIXME handle NULL result */
  657. grub_util_info ("dest_ofpath is `%s'", dest_ofpath);
  658. strncpy (boot_devpath, dest_ofpath,
  659. GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  660. - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1);
  661. boot_devpath[GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  662. - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1] = 0;
  663. free (dest_ofpath);
  664. }
  665. else
  666. {
  667. grub_util_info ("non cross-disk install");
  668. memset (boot_devpath, 0, GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  669. - GRUB_BOOT_MACHINE_BOOT_DEVPATH);
  670. }
  671. grub_util_info ("boot device path %s", boot_devpath);
  672. }
  673. #endif
  674. write_rootdev (root_dev, boot_img, bl.first_sector);
  675. /* Write the first two sectors of the core image onto the disk. */
  676. grub_util_info ("opening the core image `%s'", core_path);
  677. fp = grub_util_fd_open (core_path, GRUB_UTIL_FD_O_WRONLY);
  678. if (! GRUB_UTIL_FD_IS_VALID (fp))
  679. grub_util_error (_("cannot open `%s': %s"), core_path,
  680. grub_util_fd_strerror ());
  681. if (grub_util_fd_write (fp, core_img, GRUB_DISK_SECTOR_SIZE * 2)
  682. != GRUB_DISK_SECTOR_SIZE * 2)
  683. grub_util_error (_("cannot write to `%s': %s"),
  684. core_path, strerror (errno));
  685. if (grub_util_fd_sync (fp) < 0)
  686. grub_util_error (_("cannot sync `%s': %s"), core_path, strerror (errno));
  687. if (grub_util_fd_close (fp) < 0)
  688. grub_util_error (_("cannot close `%s': %s"), core_path, strerror (errno));
  689. grub_util_biosdisk_flush (root_dev->disk);
  690. grub_disk_cache_invalidate_all ();
  691. {
  692. char *buf, *ptr = core_img;
  693. size_t len = core_size;
  694. grub_uint64_t blk, offset = 0;
  695. grub_partition_t container = core_dev->disk->partition;
  696. grub_err_t err;
  697. core_dev->disk->partition = 0;
  698. #ifdef GRUB_SETUP_SPARC64
  699. offset = bl.gpt_offset;
  700. #endif
  701. buf = xmalloc (core_size);
  702. blk = bl.first_sector;
  703. err = grub_disk_read (core_dev->disk, blk + offset, 0, GRUB_DISK_SECTOR_SIZE, buf);
  704. if (err)
  705. grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
  706. grub_errmsg);
  707. if (grub_memcmp (buf, ptr, GRUB_DISK_SECTOR_SIZE) != 0)
  708. grub_util_error ("%s", _("blocklists are invalid"));
  709. ptr += GRUB_DISK_SECTOR_SIZE;
  710. len -= GRUB_DISK_SECTOR_SIZE;
  711. bl.block = bl.first_block;
  712. while (bl.block->len)
  713. {
  714. size_t cur = grub_target_to_host16 (bl.block->len) << GRUB_DISK_SECTOR_BITS;
  715. blk = grub_target_to_host64 (bl.block->start);
  716. if (cur > len)
  717. cur = len;
  718. err = grub_disk_read (core_dev->disk, blk + offset, 0, cur, buf);
  719. if (err)
  720. grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
  721. grub_errmsg);
  722. if (grub_memcmp (buf, ptr, cur) != 0)
  723. grub_util_error ("%s", _("blocklists are invalid"));
  724. ptr += cur;
  725. len -= cur;
  726. bl.block--;
  727. if ((char *) bl.block <= core_img)
  728. grub_util_error ("%s", _("no terminator in the core image"));
  729. }
  730. if (len)
  731. grub_util_error ("%s", _("blocklists are incomplete"));
  732. core_dev->disk->partition = container;
  733. free (buf);
  734. }
  735. #ifdef GRUB_SETUP_BIOS
  736. finish:
  737. #endif
  738. /* Write the boot image onto the disk. */
  739. if (grub_disk_write (dest_dev->disk, BOOT_SECTOR,
  740. 0, GRUB_DISK_SECTOR_SIZE, boot_img))
  741. grub_util_error ("%s", grub_errmsg);
  742. #ifdef GRUB_SETUP_SPARC64
  743. finish:
  744. #endif
  745. grub_util_biosdisk_flush (root_dev->disk);
  746. grub_util_biosdisk_flush (dest_dev->disk);
  747. free (core_path);
  748. free (core_img);
  749. free (boot_img);
  750. grub_device_close (dest_dev);
  751. grub_device_close (root_dev);
  752. }