chainloader.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* chainloader.c - boot another boot loader */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,2007,2009,2010 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 <grub/loader.h>
  20. #include <grub/machine/chainloader.h>
  21. #include <grub/machine/biosdisk.h>
  22. #include <grub/machine/memory.h>
  23. #include <grub/file.h>
  24. #include <grub/err.h>
  25. #include <grub/device.h>
  26. #include <grub/disk.h>
  27. #include <grub/misc.h>
  28. #include <grub/types.h>
  29. #include <grub/partition.h>
  30. #include <grub/memory.h>
  31. #include <grub/dl.h>
  32. #include <grub/command.h>
  33. #include <grub/msdos_partition.h>
  34. #include <grub/machine/biosnum.h>
  35. #include <grub/cpu/floppy.h>
  36. #include <grub/i18n.h>
  37. #include <grub/video.h>
  38. #include <grub/mm.h>
  39. #include <grub/fat.h>
  40. #include <grub/ntfs.h>
  41. #include <grub/i386/relocator.h>
  42. GRUB_MOD_LICENSE ("GPLv3+");
  43. static grub_dl_t my_mod;
  44. static int boot_drive;
  45. static grub_addr_t boot_part_addr;
  46. static struct grub_relocator *rel;
  47. typedef enum
  48. {
  49. GRUB_CHAINLOADER_FORCE = 0x1,
  50. GRUB_CHAINLOADER_BPB = 0x2,
  51. } grub_chainloader_flags_t;
  52. static grub_err_t
  53. grub_chainloader_boot (void)
  54. {
  55. struct grub_relocator16_state state = {
  56. .edx = boot_drive,
  57. .esi = boot_part_addr,
  58. .ds = 0,
  59. .es = 0,
  60. .fs = 0,
  61. .gs = 0,
  62. .ss = 0,
  63. .cs = 0,
  64. .sp = GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR,
  65. .ip = GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR,
  66. .a20 = 0
  67. };
  68. grub_video_set_mode ("text", 0, 0);
  69. return grub_relocator16_boot (rel, state);
  70. }
  71. static grub_err_t
  72. grub_chainloader_unload (void)
  73. {
  74. grub_relocator_unload (rel);
  75. rel = NULL;
  76. grub_dl_unref (my_mod);
  77. return GRUB_ERR_NONE;
  78. }
  79. void
  80. grub_chainloader_patch_bpb (void *bs, grub_device_t dev, grub_uint8_t dl)
  81. {
  82. grub_uint32_t part_start = 0, heads = 0, sectors = 0;
  83. if (dev && dev->disk)
  84. {
  85. part_start = grub_partition_get_start (dev->disk->partition);
  86. if (dev->disk->data)
  87. {
  88. heads = ((struct grub_biosdisk_data *)(dev->disk->data))->heads;
  89. sectors = ((struct grub_biosdisk_data *)(dev->disk->data))->sectors;
  90. }
  91. }
  92. if (grub_memcmp ((char *) &((struct grub_ntfs_bpb *) bs)->oem_name,
  93. "NTFS", 4) == 0)
  94. {
  95. struct grub_ntfs_bpb *bpb = (struct grub_ntfs_bpb *) bs;
  96. bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
  97. bpb->bios_drive = dl;
  98. return;
  99. }
  100. do
  101. {
  102. struct grub_fat_bpb *bpb = (struct grub_fat_bpb *) bs;
  103. if (grub_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT12", 5)
  104. && grub_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT16", 5)
  105. && grub_strncmp((const char *) bpb->version_specific.fat32.fstype, "FAT32", 5))
  106. break;
  107. if (grub_le_to_cpu16 (bpb->bytes_per_sector) < 512
  108. || (grub_le_to_cpu16 (bpb->bytes_per_sector)
  109. & (grub_le_to_cpu16 (bpb->bytes_per_sector) - 1)))
  110. break;
  111. if (bpb->sectors_per_cluster == 0
  112. || (bpb->sectors_per_cluster & (bpb->sectors_per_cluster - 1)))
  113. break;
  114. if (bpb->num_reserved_sectors == 0)
  115. break;
  116. if (bpb->num_total_sectors_16 == 0 && bpb->num_total_sectors_32 == 0)
  117. break;
  118. if (bpb->num_fats == 0)
  119. break;
  120. if (bpb->sectors_per_fat_16)
  121. {
  122. bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
  123. bpb->version_specific.fat12_or_fat16.num_ph_drive = dl;
  124. if (sectors)
  125. bpb->sectors_per_track = grub_cpu_to_le16 (sectors);
  126. if (heads)
  127. bpb->num_heads = grub_cpu_to_le16 (heads);
  128. return;
  129. }
  130. if (bpb->version_specific.fat32.sectors_per_fat_32)
  131. {
  132. bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
  133. bpb->version_specific.fat32.num_ph_drive = dl;
  134. if (sectors)
  135. bpb->sectors_per_track = grub_cpu_to_le16 (sectors);
  136. if (heads)
  137. bpb->num_heads = grub_cpu_to_le16 (heads);
  138. return;
  139. }
  140. break;
  141. }
  142. while (0);
  143. }
  144. static void
  145. grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)
  146. {
  147. grub_file_t file = 0;
  148. grub_uint16_t signature;
  149. grub_device_t dev;
  150. int drive = -1;
  151. grub_addr_t part_addr = 0;
  152. grub_uint8_t *bs, *ptable;
  153. rel = grub_relocator_new ();
  154. if (!rel)
  155. goto fail;
  156. grub_dl_ref (my_mod);
  157. file = grub_file_open (filename, GRUB_FILE_TYPE_PCCHAINLOADER
  158. | GRUB_FILE_TYPE_NO_DECOMPRESS);
  159. if (! file)
  160. goto fail;
  161. {
  162. grub_relocator_chunk_t ch;
  163. grub_err_t err;
  164. err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,
  165. GRUB_DISK_SECTOR_SIZE);
  166. if (err)
  167. goto fail;
  168. bs = get_virtual_current_address (ch);
  169. err = grub_relocator_alloc_chunk_addr (rel, &ch,
  170. GRUB_MEMORY_MACHINE_PART_TABLE_ADDR,
  171. 64);
  172. if (err)
  173. goto fail;
  174. ptable = get_virtual_current_address (ch);
  175. }
  176. /* Read the first block. */
  177. if (grub_file_read (file, bs, GRUB_DISK_SECTOR_SIZE)
  178. != GRUB_DISK_SECTOR_SIZE)
  179. {
  180. if (!grub_errno)
  181. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  182. filename);
  183. goto fail;
  184. }
  185. /* Check the signature. */
  186. signature = *((grub_uint16_t *) (bs + GRUB_DISK_SECTOR_SIZE - 2));
  187. if (signature != grub_le_to_cpu16 (0xaa55)
  188. && ! (flags & GRUB_CHAINLOADER_FORCE))
  189. {
  190. grub_error (GRUB_ERR_BAD_OS, "invalid signature");
  191. goto fail;
  192. }
  193. grub_file_close (file);
  194. /* Obtain the partition table from the root device. */
  195. drive = grub_get_root_biosnumber ();
  196. dev = grub_device_open (0);
  197. if (dev && dev->disk && dev->disk->partition)
  198. {
  199. grub_disk_t disk = dev->disk;
  200. if (disk)
  201. {
  202. grub_partition_t p = disk->partition;
  203. if (p && grub_strcmp (p->partmap->name, "msdos") == 0)
  204. {
  205. disk->partition = p->parent;
  206. grub_disk_read (disk, p->offset, 446, 64, ptable);
  207. part_addr = (GRUB_MEMORY_MACHINE_PART_TABLE_ADDR
  208. + (p->index << 4));
  209. disk->partition = p;
  210. }
  211. }
  212. }
  213. if (flags & GRUB_CHAINLOADER_BPB)
  214. grub_chainloader_patch_bpb ((void *) 0x7C00, dev, drive);
  215. if (dev)
  216. grub_device_close (dev);
  217. /* Ignore errors. Perhaps it's not fatal. */
  218. grub_errno = GRUB_ERR_NONE;
  219. boot_drive = drive;
  220. boot_part_addr = part_addr;
  221. grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 1);
  222. return;
  223. fail:
  224. if (file)
  225. grub_file_close (file);
  226. grub_dl_unref (my_mod);
  227. }
  228. static grub_err_t
  229. grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
  230. int argc, char *argv[])
  231. {
  232. grub_chainloader_flags_t flags = 0;
  233. while (argc > 0)
  234. {
  235. if (grub_strcmp (argv[0], "--force") == 0)
  236. {
  237. flags |= GRUB_CHAINLOADER_FORCE;
  238. argc--;
  239. argv++;
  240. continue;
  241. }
  242. if (grub_strcmp (argv[0], "--bpb") == 0)
  243. {
  244. flags |= GRUB_CHAINLOADER_BPB;
  245. argc--;
  246. argv++;
  247. continue;
  248. }
  249. break;
  250. }
  251. if (argc == 0)
  252. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  253. grub_chainloader_cmd (argv[0], flags);
  254. return grub_errno;
  255. }
  256. static grub_command_t cmd;
  257. GRUB_MOD_INIT(chainloader)
  258. {
  259. cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
  260. N_("[--force|--bpb] FILE"),
  261. N_("Load another boot loader."));
  262. my_mod = mod;
  263. }
  264. GRUB_MOD_FINI(chainloader)
  265. {
  266. grub_unregister_command (cmd);
  267. }