setup.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. grub_uint16_t last_length;
  130. grub_disk_addr_t first_sector;
  131. };
  132. /* Helper for setup. */
  133. static void
  134. save_blocklists (grub_disk_addr_t sector, unsigned offset, unsigned length,
  135. void *data)
  136. {
  137. struct blocklists *bl = data;
  138. struct grub_boot_blocklist *prev = bl->block + 1;
  139. grub_uint64_t seclen;
  140. grub_util_info ("saving <%" GRUB_HOST_PRIuLONG_LONG ",%u,%u>",
  141. (unsigned long long) sector, offset, length);
  142. if (bl->first_sector == (grub_disk_addr_t) -1)
  143. {
  144. if (offset != 0 || length < GRUB_DISK_SECTOR_SIZE)
  145. grub_util_error ("%s", _("the first sector of the core file is not sector-aligned"));
  146. bl->first_sector = sector;
  147. sector++;
  148. length -= GRUB_DISK_SECTOR_SIZE;
  149. if (!length)
  150. return;
  151. }
  152. if (offset != 0 || bl->last_length != 0)
  153. grub_util_error ("%s", _("non-sector-aligned data is found in the core file"));
  154. seclen = (length + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS;
  155. if (bl->block != bl->first_block
  156. && (grub_target_to_host64 (prev->start)
  157. + grub_target_to_host16 (prev->len)) == sector)
  158. {
  159. grub_uint16_t t = grub_target_to_host16 (prev->len);
  160. t += seclen;
  161. prev->len = grub_host_to_target16 (t);
  162. }
  163. else
  164. {
  165. bl->block->start = grub_host_to_target64 (sector);
  166. bl->block->len = grub_host_to_target16 (seclen);
  167. #ifdef GRUB_SETUP_BIOS
  168. bl->block->segment = grub_host_to_target16 (bl->current_segment);
  169. #endif
  170. bl->block--;
  171. if (bl->block->len)
  172. grub_util_error ("%s", _("the sectors of the core file are too fragmented"));
  173. }
  174. bl->last_length = length & (GRUB_DISK_SECTOR_SIZE - 1);
  175. #ifdef GRUB_SETUP_BIOS
  176. bl->current_segment += seclen << (GRUB_DISK_SECTOR_BITS - 4);
  177. #endif
  178. }
  179. #ifdef GRUB_SETUP_BIOS
  180. /* Context for setup/identify_partmap. */
  181. struct identify_partmap_ctx
  182. {
  183. grub_partition_map_t dest_partmap;
  184. grub_partition_t container;
  185. int multiple_partmaps;
  186. };
  187. /* Helper for setup.
  188. Unlike root_dev, with dest_dev we're interested in the partition map even
  189. if dest_dev itself is a whole disk. */
  190. static int
  191. identify_partmap (grub_disk_t disk __attribute__ ((unused)),
  192. const grub_partition_t p, void *data)
  193. {
  194. struct identify_partmap_ctx *ctx = data;
  195. if (p->parent != ctx->container)
  196. return 0;
  197. /* NetBSD and OpenBSD subpartitions have metadata inside a partition,
  198. so they are safe to ignore.
  199. */
  200. if (grub_strcmp (p->partmap->name, "netbsd") == 0
  201. || grub_strcmp (p->partmap->name, "openbsd") == 0)
  202. return 0;
  203. if (ctx->dest_partmap == NULL)
  204. {
  205. ctx->dest_partmap = p->partmap;
  206. return 0;
  207. }
  208. if (ctx->dest_partmap == p->partmap)
  209. return 0;
  210. ctx->multiple_partmaps = 1;
  211. return 1;
  212. }
  213. #endif
  214. #ifdef GRUB_SETUP_BIOS
  215. #define SETUP grub_util_bios_setup
  216. #elif GRUB_SETUP_SPARC64
  217. #define SETUP grub_util_sparc_setup
  218. #else
  219. #error "Shouldn't happen"
  220. #endif
  221. void
  222. SETUP (const char *dir,
  223. const char *boot_file, const char *core_file,
  224. const char *dest, int force,
  225. int fs_probe, int allow_floppy,
  226. int add_rs_codes __attribute__ ((unused))) /* unused on sparc64 */
  227. {
  228. char *core_path;
  229. char *boot_img, *core_img, *boot_path;
  230. char *root = 0;
  231. size_t boot_size, core_size;
  232. #ifdef GRUB_SETUP_BIOS
  233. grub_uint16_t core_sectors;
  234. #endif
  235. grub_device_t root_dev = 0, dest_dev, core_dev;
  236. grub_util_fd_t fp;
  237. struct blocklists bl;
  238. bl.first_sector = (grub_disk_addr_t) -1;
  239. #ifdef GRUB_SETUP_BIOS
  240. bl.current_segment =
  241. GRUB_BOOT_I386_PC_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
  242. #endif
  243. bl.last_length = 0;
  244. /* Read the boot image by the OS service. */
  245. boot_path = grub_util_get_path (dir, boot_file);
  246. boot_size = grub_util_get_image_size (boot_path);
  247. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  248. grub_util_error (_("the size of `%s' is not %u"),
  249. boot_path, GRUB_DISK_SECTOR_SIZE);
  250. boot_img = grub_util_read_image (boot_path);
  251. free (boot_path);
  252. core_path = grub_util_get_path (dir, core_file);
  253. core_size = grub_util_get_image_size (core_path);
  254. #ifdef GRUB_SETUP_BIOS
  255. core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
  256. >> GRUB_DISK_SECTOR_BITS);
  257. #endif
  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 ("root is `%s', dest is `%s'", root, dest);
  270. grub_util_info ("Opening dest");
  271. dest_dev = grub_device_open (dest);
  272. if (! dest_dev)
  273. grub_util_error ("%s", grub_errmsg);
  274. core_dev = dest_dev;
  275. {
  276. char **root_devices = grub_guess_root_devices (dir);
  277. char **cur;
  278. int found = 0;
  279. if (!root_devices)
  280. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"), dir);
  281. for (cur = root_devices; *cur; cur++)
  282. {
  283. char *drive;
  284. grub_device_t try_dev;
  285. drive = grub_util_get_grub_dev (*cur);
  286. if (!drive)
  287. continue;
  288. try_dev = grub_device_open (drive);
  289. if (! try_dev)
  290. {
  291. free (drive);
  292. continue;
  293. }
  294. if (!found && try_dev->disk->id == dest_dev->disk->id
  295. && try_dev->disk->dev->id == dest_dev->disk->dev->id)
  296. {
  297. if (root_dev)
  298. grub_device_close (root_dev);
  299. free (root);
  300. root_dev = try_dev;
  301. root = drive;
  302. found = 1;
  303. continue;
  304. }
  305. if (!root_dev)
  306. {
  307. root_dev = try_dev;
  308. root = drive;
  309. continue;
  310. }
  311. grub_device_close (try_dev);
  312. free (drive);
  313. }
  314. if (!root_dev)
  315. {
  316. grub_util_error ("guessing the root device failed, because of `%s'",
  317. grub_errmsg);
  318. }
  319. grub_util_info ("guessed root_dev `%s' from "
  320. "dir `%s'", root_dev->disk->name, dir);
  321. for (cur = root_devices; *cur; cur++)
  322. free (*cur);
  323. free (root_devices);
  324. }
  325. grub_util_info ("setting the root device to `%s'", root);
  326. if (grub_env_set ("root", root) != GRUB_ERR_NONE)
  327. grub_util_error ("%s", grub_errmsg);
  328. #ifdef GRUB_SETUP_BIOS
  329. {
  330. char *tmp_img;
  331. grub_uint8_t *boot_drive_check;
  332. /* Read the original sector from the disk. */
  333. tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
  334. if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
  335. grub_util_error ("%s", grub_errmsg);
  336. boot_drive_check = (grub_uint8_t *) (boot_img
  337. + GRUB_BOOT_MACHINE_DRIVE_CHECK);
  338. /* Copy the possible DOS BPB. */
  339. memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
  340. tmp_img + GRUB_BOOT_MACHINE_BPB_START,
  341. GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
  342. /* If DEST_DRIVE is a hard disk, enable the workaround, which is
  343. for buggy BIOSes which don't pass boot drive correctly. Instead,
  344. they pass 0x00 or 0x01 even when booted from 0x80. */
  345. if (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk))
  346. {
  347. /* Replace the jmp (2 bytes) with double nop's. */
  348. boot_drive_check[0] = 0x90;
  349. boot_drive_check[1] = 0x90;
  350. }
  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. /* Copy the partition table. */
  364. if (ctx.dest_partmap ||
  365. (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk)))
  366. memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  367. tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  368. GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
  369. free (tmp_img);
  370. if (ctx.container
  371. && grub_strcmp (ctx.container->partmap->name, "msdos") == 0
  372. && ctx.dest_partmap
  373. && (ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_NETBSD
  374. || ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_OPENBSD))
  375. {
  376. 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."));
  377. goto unable_to_embed;
  378. }
  379. fs = grub_fs_probe (dest_dev);
  380. if (!fs)
  381. grub_errno = GRUB_ERR_NONE;
  382. is_ldm = grub_util_is_ldm (dest_dev->disk);
  383. if (fs_probe)
  384. {
  385. if (!fs && !ctx.dest_partmap)
  386. grub_util_error (_("unable to identify a filesystem in %s; safety check can't be performed"),
  387. dest_dev->disk->name);
  388. if (fs && !fs->reserved_first_sector)
  389. /* TRANSLATORS: Filesystem may reserve the space just GRUB isn't sure about it. */
  390. grub_util_error (_("%s appears to contain a %s filesystem which isn't known to "
  391. "reserve space for DOS-style boot. Installing GRUB there could "
  392. "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
  393. "by grub-setup (--skip-fs-probe disables this "
  394. "check, use at your own risk)"), dest_dev->disk->name, fs->name);
  395. if (ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
  396. && strcmp (ctx.dest_partmap->name, "gpt") != 0
  397. && strcmp (ctx.dest_partmap->name, "bsd") != 0
  398. && strcmp (ctx.dest_partmap->name, "netbsd") != 0
  399. && strcmp (ctx.dest_partmap->name, "openbsd") != 0
  400. && strcmp (ctx.dest_partmap->name, "sunpc") != 0)
  401. /* TRANSLATORS: Partition map may reserve the space just GRUB isn't sure about it. */
  402. grub_util_error (_("%s appears to contain a %s partition map which isn't known to "
  403. "reserve space for DOS-style boot. Installing GRUB there could "
  404. "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
  405. "by grub-setup (--skip-fs-probe disables this "
  406. "check, use at your own risk)"), dest_dev->disk->name, ctx.dest_partmap->name);
  407. if (is_ldm && ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
  408. && strcmp (ctx.dest_partmap->name, "gpt") != 0)
  409. grub_util_error (_("%s appears to contain a %s partition map and "
  410. "LDM which isn't known to be a safe combination."
  411. " Installing GRUB there could "
  412. "result in FILESYSTEM DESTRUCTION if valuable data"
  413. " is overwritten "
  414. "by grub-setup (--skip-fs-probe disables this "
  415. "check, use at your own risk)"),
  416. dest_dev->disk->name, ctx.dest_partmap->name);
  417. }
  418. if (! ctx.dest_partmap && ! fs && !is_ldm)
  419. {
  420. grub_util_warn ("%s", _("Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea."));
  421. goto unable_to_embed;
  422. }
  423. if (ctx.multiple_partmaps || (ctx.dest_partmap && fs) || (is_ldm && fs))
  424. {
  425. grub_util_warn ("%s", _("Attempting to install GRUB to a disk with multiple partition labels. This is not supported yet."));
  426. goto unable_to_embed;
  427. }
  428. if (ctx.dest_partmap && !ctx.dest_partmap->embed)
  429. {
  430. grub_util_warn (_("Partition style `%s' doesn't support embedding"),
  431. ctx.dest_partmap->name);
  432. goto unable_to_embed;
  433. }
  434. if (fs && !fs->embed)
  435. {
  436. grub_util_warn (_("File system `%s' doesn't support embedding"),
  437. fs->name);
  438. goto unable_to_embed;
  439. }
  440. nsec = core_sectors;
  441. if (add_rs_codes)
  442. maxsec = 2 * core_sectors;
  443. else
  444. maxsec = core_sectors;
  445. if (maxsec > ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
  446. >> GRUB_DISK_SECTOR_BITS))
  447. maxsec = ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
  448. >> GRUB_DISK_SECTOR_BITS);
  449. if (is_ldm)
  450. err = grub_util_ldm_embed (dest_dev->disk, &nsec, maxsec,
  451. GRUB_EMBED_PCBIOS, &sectors);
  452. else if (ctx.dest_partmap)
  453. err = ctx.dest_partmap->embed (dest_dev->disk, &nsec, maxsec,
  454. GRUB_EMBED_PCBIOS, &sectors);
  455. else
  456. err = fs->embed (dest_dev, &nsec, maxsec,
  457. GRUB_EMBED_PCBIOS, &sectors);
  458. if (!err && nsec < core_sectors)
  459. {
  460. err = grub_error (GRUB_ERR_OUT_OF_RANGE,
  461. N_("Your embedding area is unusually small. "
  462. "core.img won't fit in it."));
  463. }
  464. if (err)
  465. {
  466. grub_util_warn ("%s", grub_errmsg);
  467. grub_errno = GRUB_ERR_NONE;
  468. goto unable_to_embed;
  469. }
  470. assert (nsec <= maxsec);
  471. /* Clean out the blocklists. */
  472. bl.block = bl.first_block;
  473. while (bl.block->len)
  474. {
  475. grub_memset (bl.block, 0, sizeof (*bl.block));
  476. bl.block--;
  477. if ((char *) bl.block <= core_img)
  478. grub_util_error ("%s", _("no terminator in the core image"));
  479. }
  480. bl.block = bl.first_block;
  481. for (i = 0; i < nsec; i++)
  482. save_blocklists (sectors[i] + grub_partition_get_start (ctx.container),
  483. 0, GRUB_DISK_SECTOR_SIZE, &bl);
  484. /* Make sure that the last blocklist is a terminator. */
  485. if (bl.block == bl.first_block)
  486. bl.block--;
  487. bl.block->start = 0;
  488. bl.block->len = 0;
  489. bl.block->segment = 0;
  490. write_rootdev (root_dev, boot_img, bl.first_sector);
  491. /* Round up to the nearest sector boundary, and zero the extra memory */
  492. core_img = xrealloc (core_img, nsec * GRUB_DISK_SECTOR_SIZE);
  493. assert (core_img && (nsec * GRUB_DISK_SECTOR_SIZE >= core_size));
  494. memset (core_img + core_size, 0, nsec * GRUB_DISK_SECTOR_SIZE - core_size);
  495. bl.first_block = (struct grub_boot_blocklist *) (core_img
  496. + GRUB_DISK_SECTOR_SIZE
  497. - sizeof (*bl.block));
  498. grub_size_t no_rs_length;
  499. no_rs_length = grub_target_to_host16
  500. (grub_get_unaligned16 (core_img
  501. + GRUB_DISK_SECTOR_SIZE
  502. + GRUB_KERNEL_I386_PC_NO_REED_SOLOMON_LENGTH));
  503. if (no_rs_length == 0xffff)
  504. grub_util_error ("%s", _("core.img version mismatch"));
  505. if (add_rs_codes)
  506. {
  507. grub_set_unaligned32 ((core_img + GRUB_DISK_SECTOR_SIZE
  508. + GRUB_KERNEL_I386_PC_REED_SOLOMON_REDUNDANCY),
  509. grub_host_to_target32 (nsec * GRUB_DISK_SECTOR_SIZE - core_size));
  510. void *tmp = xmalloc (core_size);
  511. grub_memcpy (tmp, core_img, core_size);
  512. grub_reed_solomon_add_redundancy (core_img + no_rs_length + GRUB_DISK_SECTOR_SIZE,
  513. core_size - no_rs_length - GRUB_DISK_SECTOR_SIZE,
  514. nsec * GRUB_DISK_SECTOR_SIZE
  515. - core_size);
  516. assert (grub_memcmp (tmp, core_img, core_size) == 0);
  517. free (tmp);
  518. }
  519. /* Write the core image onto the disk. */
  520. for (i = 0; i < nsec; i++)
  521. grub_disk_write (dest_dev->disk, sectors[i], 0,
  522. GRUB_DISK_SECTOR_SIZE,
  523. core_img + i * GRUB_DISK_SECTOR_SIZE);
  524. grub_free (sectors);
  525. goto finish;
  526. }
  527. unable_to_embed:
  528. #endif
  529. if (dest_dev->disk->dev->id != root_dev->disk->dev->id)
  530. grub_util_error ("%s", _("embedding is not possible, but this is required for "
  531. "RAID and LVM install"));
  532. {
  533. grub_fs_t fs;
  534. fs = grub_fs_probe (root_dev);
  535. if (!fs)
  536. grub_util_error (_("can't determine filesystem on %s"), root);
  537. if (!fs->blocklist_install)
  538. grub_util_error (_("filesystem `%s' doesn't support blocklists"),
  539. fs->name);
  540. }
  541. #ifdef GRUB_SETUP_BIOS
  542. if (dest_dev->disk->id != root_dev->disk->id
  543. || dest_dev->disk->dev->id != root_dev->disk->dev->id)
  544. /* TRANSLATORS: cross-disk refers to /boot being on one disk
  545. but MBR on another. */
  546. grub_util_error ("%s", _("embedding is not possible, but this is required for "
  547. "cross-disk install"));
  548. #else
  549. core_dev = root_dev;
  550. #endif
  551. grub_util_warn ("%s", _("Embedding is not possible. GRUB can only be installed in this "
  552. "setup by using blocklists. However, blocklists are UNRELIABLE and "
  553. "their use is discouraged."));
  554. if (! force)
  555. /* TRANSLATORS: Here GRUB refuses to continue with blocklist install. */
  556. grub_util_error ("%s", _("will not proceed with blocklists"));
  557. /* The core image must be put on a filesystem unfortunately. */
  558. grub_util_info ("will leave the core image on the filesystem");
  559. grub_util_biosdisk_flush (root_dev->disk);
  560. /* Clean out the blocklists. */
  561. bl.block = bl.first_block;
  562. while (bl.block->len)
  563. {
  564. bl.block->start = 0;
  565. bl.block->len = 0;
  566. #ifdef GRUB_SETUP_BIOS
  567. bl.block->segment = 0;
  568. #endif
  569. bl.block--;
  570. if ((char *) bl.block <= core_img)
  571. grub_util_error ("%s", _("no terminator in the core image"));
  572. }
  573. bl.block = bl.first_block;
  574. grub_install_get_blocklist (root_dev, core_path, core_img, core_size,
  575. save_blocklists, &bl);
  576. if (bl.first_sector == (grub_disk_addr_t)-1)
  577. grub_util_error ("%s", _("can't retrieve blocklists"));
  578. #ifdef GRUB_SETUP_SPARC64
  579. {
  580. char *boot_devpath;
  581. boot_devpath = (char *) (boot_img
  582. + GRUB_BOOT_AOUT_HEADER_SIZE
  583. + GRUB_BOOT_MACHINE_BOOT_DEVPATH);
  584. if (dest_dev->disk->id != root_dev->disk->id
  585. || dest_dev->disk->dev->id != root_dev->disk->dev->id)
  586. {
  587. char *dest_ofpath;
  588. dest_ofpath
  589. = grub_util_devname_to_ofpath (grub_util_biosdisk_get_osdev (root_dev->disk));
  590. /* FIXME handle NULL result */
  591. grub_util_info ("dest_ofpath is `%s'", dest_ofpath);
  592. strncpy (boot_devpath, dest_ofpath,
  593. GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  594. - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1);
  595. boot_devpath[GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  596. - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1] = 0;
  597. free (dest_ofpath);
  598. }
  599. else
  600. {
  601. grub_util_info ("non cross-disk install");
  602. memset (boot_devpath, 0, GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  603. - GRUB_BOOT_MACHINE_BOOT_DEVPATH);
  604. }
  605. grub_util_info ("boot device path %s", boot_devpath);
  606. }
  607. #endif
  608. write_rootdev (root_dev, boot_img, bl.first_sector);
  609. /* Write the first two sectors of the core image onto the disk. */
  610. grub_util_info ("opening the core image `%s'", core_path);
  611. fp = grub_util_fd_open (core_path, GRUB_UTIL_FD_O_WRONLY);
  612. if (! GRUB_UTIL_FD_IS_VALID (fp))
  613. grub_util_error (_("cannot open `%s': %s"), core_path,
  614. grub_util_fd_strerror ());
  615. if (grub_util_fd_write (fp, core_img, GRUB_DISK_SECTOR_SIZE * 2)
  616. != GRUB_DISK_SECTOR_SIZE * 2)
  617. grub_util_error (_("cannot write to `%s': %s"),
  618. core_path, strerror (errno));
  619. grub_util_fd_sync (fp);
  620. grub_util_fd_close (fp);
  621. grub_util_biosdisk_flush (root_dev->disk);
  622. grub_disk_cache_invalidate_all ();
  623. {
  624. char *buf, *ptr = core_img;
  625. size_t len = core_size;
  626. grub_uint64_t blk;
  627. grub_partition_t container = core_dev->disk->partition;
  628. grub_err_t err;
  629. core_dev->disk->partition = 0;
  630. buf = xmalloc (core_size);
  631. blk = bl.first_sector;
  632. err = grub_disk_read (core_dev->disk, blk, 0, GRUB_DISK_SECTOR_SIZE, buf);
  633. if (err)
  634. grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
  635. grub_errmsg);
  636. if (grub_memcmp (buf, ptr, GRUB_DISK_SECTOR_SIZE) != 0)
  637. grub_util_error ("%s", _("blocklists are invalid"));
  638. ptr += GRUB_DISK_SECTOR_SIZE;
  639. len -= GRUB_DISK_SECTOR_SIZE;
  640. bl.block = bl.first_block;
  641. while (bl.block->len)
  642. {
  643. size_t cur = grub_target_to_host16 (bl.block->len) << GRUB_DISK_SECTOR_BITS;
  644. blk = grub_target_to_host64 (bl.block->start);
  645. if (cur > len)
  646. cur = len;
  647. err = grub_disk_read (core_dev->disk, blk, 0, cur, buf);
  648. if (err)
  649. grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
  650. grub_errmsg);
  651. if (grub_memcmp (buf, ptr, cur) != 0)
  652. grub_util_error ("%s", _("blocklists are invalid"));
  653. ptr += cur;
  654. len -= cur;
  655. bl.block--;
  656. if ((char *) bl.block <= core_img)
  657. grub_util_error ("%s", _("no terminator in the core image"));
  658. }
  659. if (len)
  660. grub_util_error ("%s", _("blocklists are incomplete"));
  661. core_dev->disk->partition = container;
  662. free (buf);
  663. }
  664. #ifdef GRUB_SETUP_BIOS
  665. finish:
  666. #endif
  667. /* Write the boot image onto the disk. */
  668. if (grub_disk_write (dest_dev->disk, BOOT_SECTOR,
  669. 0, GRUB_DISK_SECTOR_SIZE, boot_img))
  670. grub_util_error ("%s", grub_errmsg);
  671. grub_util_biosdisk_flush (root_dev->disk);
  672. grub_util_biosdisk_flush (dest_dev->disk);
  673. free (core_path);
  674. free (core_img);
  675. free (boot_img);
  676. grub_device_close (dest_dev);
  677. grub_device_close (root_dev);
  678. }