setup.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. int warn_small)
  232. {
  233. char *core_path;
  234. char *boot_img, *core_img, *boot_path;
  235. char *root = 0;
  236. size_t boot_size, core_size;
  237. grub_uint16_t core_sectors;
  238. grub_device_t root_dev = 0, dest_dev, core_dev;
  239. grub_util_fd_t fp;
  240. struct blocklists bl;
  241. bl.first_sector = (grub_disk_addr_t) -1;
  242. #ifdef GRUB_SETUP_BIOS
  243. bl.current_segment =
  244. GRUB_BOOT_I386_PC_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >> 4);
  245. #endif
  246. #ifdef GRUB_SETUP_SPARC64
  247. bl.gpt_offset = 0;
  248. #endif
  249. bl.last_length = 0;
  250. /* Read the boot image by the OS service. */
  251. boot_path = grub_util_get_path (dir, boot_file);
  252. boot_size = grub_util_get_image_size (boot_path);
  253. if (boot_size != GRUB_DISK_SECTOR_SIZE)
  254. grub_util_error (_("the size of `%s' is not %u"),
  255. boot_path, GRUB_DISK_SECTOR_SIZE);
  256. boot_img = grub_util_read_image (boot_path);
  257. free (boot_path);
  258. core_path = grub_util_get_path (dir, core_file);
  259. core_size = grub_util_get_image_size (core_path);
  260. core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
  261. >> GRUB_DISK_SECTOR_BITS);
  262. if (core_size < GRUB_DISK_SECTOR_SIZE)
  263. grub_util_error (_("the size of `%s' is too small"), core_path);
  264. #ifdef GRUB_SETUP_BIOS
  265. if (core_size > 0xFFFF * GRUB_DISK_SECTOR_SIZE)
  266. grub_util_error (_("the size of `%s' is too large"), core_path);
  267. #endif
  268. core_img = grub_util_read_image (core_path);
  269. /* Have FIRST_BLOCK to point to the first blocklist. */
  270. bl.first_block = (struct grub_boot_blocklist *) (core_img
  271. + GRUB_DISK_SECTOR_SIZE
  272. - sizeof (*bl.block));
  273. grub_util_info ("Opening dest `%s'", dest);
  274. dest_dev = grub_device_open (dest);
  275. if (! dest_dev)
  276. grub_util_error ("%s", grub_errmsg);
  277. core_dev = dest_dev;
  278. {
  279. char **root_devices = grub_guess_root_devices (dir);
  280. char **cur;
  281. int found = 0;
  282. if (!root_devices)
  283. grub_util_error (_("cannot find a device for %s (is /dev mounted?)"), dir);
  284. for (cur = root_devices; *cur; cur++)
  285. {
  286. char *drive;
  287. grub_device_t try_dev;
  288. drive = grub_util_get_grub_dev (*cur);
  289. if (!drive)
  290. continue;
  291. try_dev = grub_device_open (drive);
  292. if (! try_dev)
  293. {
  294. free (drive);
  295. continue;
  296. }
  297. if (!found && try_dev->disk->id == dest_dev->disk->id
  298. && try_dev->disk->dev->id == dest_dev->disk->dev->id)
  299. {
  300. if (root_dev)
  301. grub_device_close (root_dev);
  302. free (root);
  303. root_dev = try_dev;
  304. root = drive;
  305. found = 1;
  306. continue;
  307. }
  308. if (!root_dev)
  309. {
  310. root_dev = try_dev;
  311. root = drive;
  312. continue;
  313. }
  314. grub_device_close (try_dev);
  315. free (drive);
  316. }
  317. if (!root_dev)
  318. {
  319. grub_util_error ("guessing the root device failed, because of `%s'",
  320. grub_errmsg);
  321. }
  322. grub_util_info ("guessed root_dev `%s' from "
  323. "dir `%s'", root_dev->disk->name, dir);
  324. for (cur = root_devices; *cur; cur++)
  325. free (*cur);
  326. free (root_devices);
  327. }
  328. grub_util_info ("setting the root device to `%s'", root);
  329. if (grub_env_set ("root", root) != GRUB_ERR_NONE)
  330. grub_util_error ("%s", grub_errmsg);
  331. {
  332. #ifdef GRUB_SETUP_BIOS
  333. char *tmp_img;
  334. grub_uint8_t *boot_drive_check;
  335. /* Read the original sector from the disk. */
  336. tmp_img = xmalloc (GRUB_DISK_SECTOR_SIZE);
  337. if (grub_disk_read (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, tmp_img))
  338. grub_util_error ("%s", grub_errmsg);
  339. boot_drive_check = (grub_uint8_t *) (boot_img
  340. + GRUB_BOOT_MACHINE_DRIVE_CHECK);
  341. /* Copy the possible DOS BPB. */
  342. memcpy (boot_img + GRUB_BOOT_MACHINE_BPB_START,
  343. tmp_img + GRUB_BOOT_MACHINE_BPB_START,
  344. GRUB_BOOT_MACHINE_BPB_END - GRUB_BOOT_MACHINE_BPB_START);
  345. /* If DEST_DRIVE is a hard disk, enable the workaround, which is
  346. for buggy BIOSes which don't pass boot drive correctly. Instead,
  347. they pass 0x00 or 0x01 even when booted from 0x80. */
  348. if (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk))
  349. {
  350. /* Replace the jmp (2 bytes) with double nop's. */
  351. boot_drive_check[0] = 0x90;
  352. boot_drive_check[1] = 0x90;
  353. }
  354. #endif
  355. struct identify_partmap_ctx ctx = {
  356. .dest_partmap = NULL,
  357. .container = dest_dev->disk->partition,
  358. .multiple_partmaps = 0
  359. };
  360. int is_ldm;
  361. grub_err_t err;
  362. grub_disk_addr_t *sectors;
  363. int i;
  364. grub_fs_t fs;
  365. unsigned int nsec, maxsec;
  366. grub_partition_iterate (dest_dev->disk, identify_partmap, &ctx);
  367. #ifdef GRUB_SETUP_BIOS
  368. /* Copy the partition table. */
  369. if (ctx.dest_partmap ||
  370. (!allow_floppy && !grub_util_biosdisk_is_floppy (dest_dev->disk)))
  371. memcpy (boot_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  372. tmp_img + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC,
  373. GRUB_BOOT_MACHINE_PART_END - GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC);
  374. free (tmp_img);
  375. #endif
  376. if (ctx.container
  377. && grub_strcmp (ctx.container->partmap->name, "msdos") == 0
  378. && ctx.dest_partmap
  379. && (ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_NETBSD
  380. || ctx.container->msdostype == GRUB_PC_PARTITION_TYPE_OPENBSD))
  381. {
  382. 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."));
  383. goto unable_to_embed;
  384. }
  385. fs = grub_fs_probe (dest_dev);
  386. if (!fs)
  387. grub_errno = GRUB_ERR_NONE;
  388. is_ldm = grub_util_is_ldm (dest_dev->disk);
  389. if (fs_probe)
  390. {
  391. if (!fs && !ctx.dest_partmap)
  392. grub_util_error (_("unable to identify a filesystem in %s; safety check can't be performed"),
  393. dest_dev->disk->name);
  394. if (fs && !fs->reserved_first_sector)
  395. /* TRANSLATORS: Filesystem may reserve the space just GRUB isn't sure about it. */
  396. grub_util_error (_("%s appears to contain a %s filesystem which isn't known to "
  397. "reserve space for DOS-style boot. Installing GRUB there could "
  398. "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
  399. "by grub-setup (--skip-fs-probe disables this "
  400. "check, use at your own risk)"), dest_dev->disk->name, fs->name);
  401. if (ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
  402. && strcmp (ctx.dest_partmap->name, "gpt") != 0
  403. && strcmp (ctx.dest_partmap->name, "bsd") != 0
  404. && strcmp (ctx.dest_partmap->name, "netbsd") != 0
  405. && strcmp (ctx.dest_partmap->name, "openbsd") != 0
  406. && strcmp (ctx.dest_partmap->name, "sunpc") != 0)
  407. /* TRANSLATORS: Partition map may reserve the space just GRUB isn't sure about it. */
  408. grub_util_error (_("%s appears to contain a %s partition map which isn't known to "
  409. "reserve space for DOS-style boot. Installing GRUB there could "
  410. "result in FILESYSTEM DESTRUCTION if valuable data is overwritten "
  411. "by grub-setup (--skip-fs-probe disables this "
  412. "check, use at your own risk)"), dest_dev->disk->name, ctx.dest_partmap->name);
  413. if (is_ldm && ctx.dest_partmap && strcmp (ctx.dest_partmap->name, "msdos") != 0
  414. && strcmp (ctx.dest_partmap->name, "gpt") != 0)
  415. grub_util_error (_("%s appears to contain a %s partition map and "
  416. "LDM which isn't known to be a safe combination."
  417. " Installing GRUB there could "
  418. "result in FILESYSTEM DESTRUCTION if valuable data"
  419. " is overwritten "
  420. "by grub-setup (--skip-fs-probe disables this "
  421. "check, use at your own risk)"),
  422. dest_dev->disk->name, ctx.dest_partmap->name);
  423. }
  424. if (! ctx.dest_partmap && ! fs && !is_ldm)
  425. {
  426. grub_util_warn ("%s", _("Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea."));
  427. goto unable_to_embed;
  428. }
  429. if (ctx.multiple_partmaps || (ctx.dest_partmap && fs) || (is_ldm && fs))
  430. {
  431. grub_util_warn ("%s", _("Attempting to install GRUB to a disk with multiple partition labels. This is not supported yet."));
  432. goto unable_to_embed;
  433. }
  434. if (ctx.dest_partmap && !ctx.dest_partmap->embed)
  435. {
  436. grub_util_warn (_("Partition style `%s' doesn't support embedding"),
  437. ctx.dest_partmap->name);
  438. goto unable_to_embed;
  439. }
  440. if (fs && !fs->fs_embed)
  441. {
  442. grub_util_warn (_("File system `%s' doesn't support embedding"),
  443. fs->name);
  444. goto unable_to_embed;
  445. }
  446. nsec = core_sectors;
  447. if (add_rs_codes)
  448. maxsec = 2 * core_sectors;
  449. else
  450. maxsec = core_sectors;
  451. #ifdef GRUB_SETUP_BIOS
  452. if (maxsec > ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
  453. >> GRUB_DISK_SECTOR_BITS))
  454. maxsec = ((0x78000 - GRUB_KERNEL_I386_PC_LINK_ADDR)
  455. >> GRUB_DISK_SECTOR_BITS);
  456. #endif
  457. #ifdef GRUB_SETUP_SPARC64
  458. /*
  459. * On SPARC we need two extra. One is because we are combining the
  460. * core.img with the boot.img. The other is because the boot sector
  461. * starts at 1.
  462. */
  463. nsec += 2;
  464. maxsec += 2;
  465. #endif
  466. if (is_ldm)
  467. err = grub_util_ldm_embed (dest_dev->disk, &nsec, maxsec,
  468. GRUB_EMBED_PCBIOS, &sectors);
  469. else if (ctx.dest_partmap)
  470. err = ctx.dest_partmap->embed (dest_dev->disk, &nsec, maxsec,
  471. GRUB_EMBED_PCBIOS, &sectors, warn_small);
  472. else
  473. err = fs->fs_embed (dest_dev, &nsec, maxsec,
  474. GRUB_EMBED_PCBIOS, &sectors);
  475. if (!err && nsec < core_sectors)
  476. {
  477. err = grub_error (GRUB_ERR_OUT_OF_RANGE,
  478. N_("Your embedding area is unusually small. "
  479. "core.img won't fit in it."));
  480. }
  481. if (err)
  482. {
  483. grub_util_warn ("%s", grub_errmsg);
  484. grub_errno = GRUB_ERR_NONE;
  485. goto unable_to_embed;
  486. }
  487. assert (nsec <= maxsec);
  488. /* Clean out the blocklists. */
  489. bl.block = bl.first_block;
  490. while (bl.block->len)
  491. {
  492. grub_memset (bl.block, 0, sizeof (*bl.block));
  493. bl.block--;
  494. if ((char *) bl.block <= core_img)
  495. grub_util_error ("%s", _("no terminator in the core image"));
  496. }
  497. bl.block = bl.first_block;
  498. for (i = 0; i < nsec; i++)
  499. save_blocklists (sectors[i] + grub_partition_get_start (ctx.container),
  500. 0, GRUB_DISK_SECTOR_SIZE, &bl);
  501. /* Make sure that the last blocklist is a terminator. */
  502. if (bl.block == bl.first_block)
  503. bl.block--;
  504. bl.block->start = 0;
  505. bl.block->len = 0;
  506. #ifdef GRUB_SETUP_BIOS
  507. bl.block->segment = 0;
  508. #endif
  509. #ifdef GRUB_SETUP_SPARC64
  510. {
  511. /*
  512. * On SPARC, the block-list entries need to be based off the beginning
  513. * of the parition, not the beginning of the disk.
  514. */
  515. struct grub_boot_blocklist *block;
  516. block = bl.first_block;
  517. while (block->len)
  518. {
  519. block->start -= bl.first_sector;
  520. block--;
  521. }
  522. }
  523. /*
  524. * Reserve space for the boot block since it can not be in the
  525. * Parition table on SPARC.
  526. */
  527. assert (bl.first_block->len > 2);
  528. bl.first_block->start += 2;
  529. bl.first_block->len -= 2;
  530. write_rootdev (root_dev, boot_img, sectors[BOOT_SECTOR + 1] - bl.first_sector);
  531. #endif
  532. #ifdef GRUB_SETUP_BIOS
  533. write_rootdev (root_dev, boot_img, bl.first_sector);
  534. #endif
  535. /* Round up to the nearest sector boundary, and zero the extra memory */
  536. core_img = xrealloc (core_img, nsec * GRUB_DISK_SECTOR_SIZE);
  537. assert (core_img && (nsec * GRUB_DISK_SECTOR_SIZE >= core_size));
  538. memset (core_img + core_size, 0, nsec * GRUB_DISK_SECTOR_SIZE - core_size);
  539. bl.first_block = (struct grub_boot_blocklist *) (core_img
  540. + GRUB_DISK_SECTOR_SIZE
  541. - sizeof (*bl.block));
  542. #if GRUB_SETUP_BIOS
  543. grub_size_t no_rs_length;
  544. no_rs_length = grub_target_to_host16
  545. (grub_get_unaligned16 (core_img
  546. + GRUB_DISK_SECTOR_SIZE
  547. + GRUB_KERNEL_I386_PC_NO_REED_SOLOMON_LENGTH));
  548. if (no_rs_length == 0xffff)
  549. grub_util_error ("%s", _("core.img version mismatch"));
  550. if (add_rs_codes)
  551. {
  552. grub_set_unaligned32 ((core_img + GRUB_DISK_SECTOR_SIZE
  553. + GRUB_KERNEL_I386_PC_REED_SOLOMON_REDUNDANCY),
  554. grub_host_to_target32 (nsec * GRUB_DISK_SECTOR_SIZE - core_size));
  555. void *tmp = xmalloc (core_size);
  556. grub_memcpy (tmp, core_img, core_size);
  557. grub_reed_solomon_add_redundancy (core_img + no_rs_length + GRUB_DISK_SECTOR_SIZE,
  558. core_size - no_rs_length - GRUB_DISK_SECTOR_SIZE,
  559. nsec * GRUB_DISK_SECTOR_SIZE
  560. - core_size);
  561. assert (grub_memcmp (tmp, core_img, core_size) == 0);
  562. free (tmp);
  563. }
  564. /* Write the core image onto the disk. */
  565. for (i = 0; i < nsec; i++)
  566. grub_disk_write (dest_dev->disk, sectors[i], 0,
  567. GRUB_DISK_SECTOR_SIZE,
  568. core_img + i * GRUB_DISK_SECTOR_SIZE);
  569. #endif
  570. #ifdef GRUB_SETUP_SPARC64
  571. {
  572. int isec = BOOT_SECTOR;
  573. /* Write the boot image onto the disk. */
  574. if (grub_disk_write (dest_dev->disk, sectors[isec++], 0,
  575. GRUB_DISK_SECTOR_SIZE, boot_img))
  576. grub_util_error ("%s", grub_errmsg);
  577. /* Write the core image onto the disk. */
  578. for (i = 0 ; isec < nsec; i++, isec++)
  579. {
  580. if (grub_disk_write (dest_dev->disk, sectors[isec], 0,
  581. GRUB_DISK_SECTOR_SIZE,
  582. core_img + i * GRUB_DISK_SECTOR_SIZE))
  583. grub_util_error ("%s", grub_errmsg);
  584. }
  585. }
  586. #endif
  587. grub_free (sectors);
  588. goto finish;
  589. }
  590. unable_to_embed:
  591. if (dest_dev->disk->dev->id != root_dev->disk->dev->id)
  592. grub_util_error ("%s", _("embedding is not possible, but this is required for "
  593. "RAID and LVM install"));
  594. {
  595. grub_fs_t fs;
  596. fs = grub_fs_probe (root_dev);
  597. if (!fs)
  598. grub_util_error (_("can't determine filesystem on %s"), root);
  599. if (!fs->blocklist_install)
  600. grub_util_error (_("filesystem `%s' doesn't support blocklists"),
  601. fs->name);
  602. }
  603. #ifdef GRUB_SETUP_BIOS
  604. if (dest_dev->disk->id != root_dev->disk->id
  605. || dest_dev->disk->dev->id != root_dev->disk->dev->id)
  606. /* TRANSLATORS: cross-disk refers to /boot being on one disk
  607. but MBR on another. */
  608. grub_util_error ("%s", _("embedding is not possible, but this is required for "
  609. "cross-disk install"));
  610. #else
  611. core_dev = root_dev;
  612. #endif
  613. grub_util_warn ("%s", _("Embedding is not possible. GRUB can only be installed in this "
  614. "setup by using blocklists. However, blocklists are UNRELIABLE and "
  615. "their use is discouraged."));
  616. if (! force)
  617. /* TRANSLATORS: Here GRUB refuses to continue with blocklist install. */
  618. grub_util_error ("%s", _("will not proceed with blocklists"));
  619. /* The core image must be put on a filesystem unfortunately. */
  620. grub_util_info ("will leave the core image on the filesystem");
  621. grub_util_biosdisk_flush (root_dev->disk);
  622. /* Clean out the blocklists. */
  623. bl.block = bl.first_block;
  624. while (bl.block->len)
  625. {
  626. bl.block->start = 0;
  627. bl.block->len = 0;
  628. #ifdef GRUB_SETUP_BIOS
  629. bl.block->segment = 0;
  630. #endif
  631. bl.block--;
  632. if ((char *) bl.block <= core_img)
  633. grub_util_error ("%s", _("no terminator in the core image"));
  634. }
  635. bl.block = bl.first_block;
  636. #ifdef GRUB_SETUP_SPARC64
  637. {
  638. grub_partition_t container = root_dev->disk->partition;
  639. if (grub_strstr (container->partmap->name, "gpt"))
  640. bl.gpt_offset = grub_partition_get_start (container);
  641. }
  642. #endif
  643. grub_install_get_blocklist (root_dev, core_path, core_img, core_size,
  644. save_blocklists, &bl);
  645. if (bl.first_sector == (grub_disk_addr_t)-1)
  646. grub_util_error ("%s", _("can't retrieve blocklists"));
  647. #ifdef GRUB_SETUP_SPARC64
  648. {
  649. char *boot_devpath;
  650. boot_devpath = (char *) (boot_img
  651. + GRUB_BOOT_AOUT_HEADER_SIZE
  652. + GRUB_BOOT_MACHINE_BOOT_DEVPATH);
  653. if (dest_dev->disk->id != root_dev->disk->id
  654. || dest_dev->disk->dev->id != root_dev->disk->dev->id)
  655. {
  656. char *dest_ofpath;
  657. dest_ofpath
  658. = grub_util_devname_to_ofpath (grub_util_biosdisk_get_osdev (root_dev->disk));
  659. /* FIXME handle NULL result */
  660. grub_util_info ("dest_ofpath is `%s'", dest_ofpath);
  661. strncpy (boot_devpath, dest_ofpath,
  662. GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  663. - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1);
  664. boot_devpath[GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  665. - GRUB_BOOT_MACHINE_BOOT_DEVPATH - 1] = 0;
  666. free (dest_ofpath);
  667. }
  668. else
  669. {
  670. grub_util_info ("non cross-disk install");
  671. memset (boot_devpath, 0, GRUB_BOOT_MACHINE_BOOT_DEVPATH_END
  672. - GRUB_BOOT_MACHINE_BOOT_DEVPATH);
  673. }
  674. grub_util_info ("boot device path %s", boot_devpath);
  675. }
  676. #endif
  677. write_rootdev (root_dev, boot_img, bl.first_sector);
  678. /* Write the first two sectors of the core image onto the disk. */
  679. grub_util_info ("opening the core image `%s'", core_path);
  680. fp = grub_util_fd_open (core_path, GRUB_UTIL_FD_O_WRONLY);
  681. if (! GRUB_UTIL_FD_IS_VALID (fp))
  682. grub_util_error (_("cannot open `%s': %s"), core_path,
  683. grub_util_fd_strerror ());
  684. if (grub_util_fd_write (fp, core_img, GRUB_DISK_SECTOR_SIZE * 2)
  685. != GRUB_DISK_SECTOR_SIZE * 2)
  686. grub_util_error (_("cannot write to `%s': %s"),
  687. core_path, strerror (errno));
  688. if (grub_util_fd_sync (fp) < 0)
  689. grub_util_error (_("cannot sync `%s': %s"), core_path, strerror (errno));
  690. if (grub_util_fd_close (fp) < 0)
  691. grub_util_error (_("cannot close `%s': %s"), core_path, strerror (errno));
  692. grub_util_biosdisk_flush (root_dev->disk);
  693. grub_disk_cache_invalidate_all ();
  694. {
  695. char *buf, *ptr = core_img;
  696. size_t len = core_size;
  697. grub_uint64_t blk, offset = 0;
  698. grub_partition_t container = core_dev->disk->partition;
  699. grub_err_t err;
  700. core_dev->disk->partition = 0;
  701. #ifdef GRUB_SETUP_SPARC64
  702. offset = bl.gpt_offset;
  703. #endif
  704. buf = xmalloc (core_size);
  705. blk = bl.first_sector;
  706. err = grub_disk_read (core_dev->disk, blk + offset, 0, GRUB_DISK_SECTOR_SIZE, buf);
  707. if (err)
  708. grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
  709. grub_errmsg);
  710. if (grub_memcmp (buf, ptr, GRUB_DISK_SECTOR_SIZE) != 0)
  711. grub_util_error ("%s", _("blocklists are invalid"));
  712. ptr += GRUB_DISK_SECTOR_SIZE;
  713. len -= GRUB_DISK_SECTOR_SIZE;
  714. bl.block = bl.first_block;
  715. while (bl.block->len)
  716. {
  717. size_t cur = grub_target_to_host16 (bl.block->len) << GRUB_DISK_SECTOR_BITS;
  718. blk = grub_target_to_host64 (bl.block->start);
  719. if (cur > len)
  720. cur = len;
  721. err = grub_disk_read (core_dev->disk, blk + offset, 0, cur, buf);
  722. if (err)
  723. grub_util_error (_("cannot read `%s': %s"), core_dev->disk->name,
  724. grub_errmsg);
  725. if (grub_memcmp (buf, ptr, cur) != 0)
  726. grub_util_error ("%s", _("blocklists are invalid"));
  727. ptr += cur;
  728. len -= cur;
  729. bl.block--;
  730. if ((char *) bl.block <= core_img)
  731. grub_util_error ("%s", _("no terminator in the core image"));
  732. }
  733. if (len)
  734. grub_util_error ("%s", _("blocklists are incomplete"));
  735. core_dev->disk->partition = container;
  736. free (buf);
  737. }
  738. #ifdef GRUB_SETUP_BIOS
  739. finish:
  740. #endif
  741. /* Write the boot image onto the disk. */
  742. if (grub_disk_write (dest_dev->disk, BOOT_SECTOR,
  743. 0, GRUB_DISK_SECTOR_SIZE, boot_img))
  744. grub_util_error ("%s", grub_errmsg);
  745. #ifdef GRUB_SETUP_SPARC64
  746. finish:
  747. #endif
  748. grub_util_biosdisk_flush (root_dev->disk);
  749. grub_util_biosdisk_flush (dest_dev->disk);
  750. free (core_path);
  751. free (core_img);
  752. free (boot_img);
  753. grub_device_close (dest_dev);
  754. grub_device_close (root_dev);
  755. }