1 /* chainloader.c - boot another boot loader */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2007,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/loader.h>
21 #include <grub/machine/chainloader.h>
22 #include <grub/machine/biosdisk.h>
23 #include <grub/machine/memory.h>
24 #include <grub/file.h>
26 #include <grub/device.h>
27 #include <grub/disk.h>
28 #include <grub/misc.h>
29 #include <grub/types.h>
30 #include <grub/partition.h>
31 #include <grub/memory.h>
33 #include <grub/command.h>
34 #include <grub/msdos_partition.h>
35 #include <grub/machine/biosnum.h>
36 #include <grub/cpu/floppy.h>
37 #include <grub/i18n.h>
38 #include <grub/video.h>
41 #include <grub/ntfs.h>
42 #include <grub/i386/relocator.h>
44 GRUB_MOD_LICENSE ("GPLv3+");
46 static grub_dl_t my_mod;
47 static int boot_drive;
48 static grub_addr_t boot_part_addr;
49 static struct grub_relocator *rel;
53 GRUB_CHAINLOADER_FORCE = 0x1,
54 GRUB_CHAINLOADER_BPB = 0x2,
55 } grub_chainloader_flags_t;
58 grub_chainloader_boot (void)
60 struct grub_relocator16_state state = {
62 .esi = boot_part_addr,
69 .sp = GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR,
70 .ip = GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR,
73 grub_video_set_mode ("text", 0, 0);
75 return grub_relocator16_boot (rel, state);
79 grub_chainloader_unload (void)
81 grub_relocator_unload (rel);
83 grub_dl_unref (my_mod);
88 grub_chainloader_patch_bpb (void *bs, grub_device_t dev, grub_uint8_t dl)
90 grub_uint32_t part_start = 0, heads = 0, sectors = 0;
93 part_start = grub_partition_get_start (dev->disk->partition);
96 heads = ((struct grub_biosdisk_data *)(dev->disk->data))->heads;
97 sectors = ((struct grub_biosdisk_data *)(dev->disk->data))->sectors;
100 if (grub_memcmp ((char *) &((struct grub_ntfs_bpb *) bs)->oem_name,
103 struct grub_ntfs_bpb *bpb = (struct grub_ntfs_bpb *) bs;
104 bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
105 bpb->bios_drive = dl;
111 struct grub_fat_bpb *bpb = (struct grub_fat_bpb *) bs;
112 if (grub_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT12", 5)
113 && grub_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT16", 5)
114 && grub_strncmp((const char *) bpb->version_specific.fat32.fstype, "FAT32", 5))
117 if (grub_le_to_cpu16 (bpb->bytes_per_sector) < 512
118 || (grub_le_to_cpu16 (bpb->bytes_per_sector)
119 & (grub_le_to_cpu16 (bpb->bytes_per_sector) - 1)))
122 if (bpb->sectors_per_cluster == 0
123 || (bpb->sectors_per_cluster & (bpb->sectors_per_cluster - 1)))
126 if (bpb->num_reserved_sectors == 0)
128 if (bpb->num_total_sectors_16 == 0 && bpb->num_total_sectors_32 == 0)
131 if (bpb->num_fats == 0)
134 if (bpb->sectors_per_fat_16)
136 bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
137 bpb->version_specific.fat12_or_fat16.num_ph_drive = dl;
139 bpb->sectors_per_track = grub_cpu_to_le16 (sectors);
141 bpb->num_heads = grub_cpu_to_le16 (heads);
144 if (bpb->version_specific.fat32.sectors_per_fat_32)
146 bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
147 bpb->version_specific.fat32.num_ph_drive = dl;
149 bpb->sectors_per_track = grub_cpu_to_le16 (sectors);
151 bpb->num_heads = grub_cpu_to_le16 (heads);
160 grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)
162 grub_file_t file = 0;
163 grub_uint16_t signature;
166 grub_addr_t part_addr = 0;
167 grub_uint8_t *bs, *ptable;
169 rel = grub_relocator_new ();
173 grub_dl_ref (my_mod);
175 grub_file_filter_disable_compression ();
176 file = grub_file_open (filename);
181 grub_relocator_chunk_t ch;
184 err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,
185 GRUB_DISK_SECTOR_SIZE);
188 bs = get_virtual_current_address (ch);
189 err = grub_relocator_alloc_chunk_addr (rel, &ch,
190 GRUB_MEMORY_MACHINE_PART_TABLE_ADDR,
194 ptable = get_virtual_current_address (ch);
197 /* Read the first block. */
198 if (grub_file_read (file, bs, GRUB_DISK_SECTOR_SIZE)
199 != GRUB_DISK_SECTOR_SIZE)
202 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
208 /* Check the signature. */
209 signature = *((grub_uint16_t *) (bs + GRUB_DISK_SECTOR_SIZE - 2));
210 if (signature != grub_le_to_cpu16 (0xaa55)
211 && ! (flags & GRUB_CHAINLOADER_FORCE))
213 grub_error (GRUB_ERR_BAD_OS, "invalid signature");
217 grub_file_close (file);
219 /* Obtain the partition table from the root device. */
220 drive = grub_get_root_biosnumber ();
221 dev = grub_device_open (0);
222 if (dev && dev->disk && dev->disk->partition)
224 grub_disk_t disk = dev->disk;
228 grub_partition_t p = disk->partition;
230 if (p && grub_strcmp (p->partmap->name, "msdos") == 0)
232 disk->partition = p->parent;
233 grub_disk_read (disk, p->offset, 446, 64, ptable);
234 part_addr = (GRUB_MEMORY_MACHINE_PART_TABLE_ADDR
241 if (flags & GRUB_CHAINLOADER_BPB)
242 grub_chainloader_patch_bpb ((void *) 0x7C00, dev, drive);
245 grub_device_close (dev);
247 /* Ignore errors. Perhaps it's not fatal. */
248 grub_errno = GRUB_ERR_NONE;
251 boot_part_addr = part_addr;
253 grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 1);
259 grub_file_close (file);
261 grub_dl_unref (my_mod);
265 grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
266 int argc, char *argv[])
268 grub_chainloader_flags_t flags = 0;
272 if (grub_strcmp (argv[0], "--force") == 0)
274 flags |= GRUB_CHAINLOADER_FORCE;
279 if (grub_strcmp (argv[0], "--bpb") == 0)
281 flags |= GRUB_CHAINLOADER_BPB;
290 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
292 grub_chainloader_cmd (argv[0], flags);
297 static grub_command_t cmd;
299 GRUB_MOD_INIT(chainloader)
301 cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
302 N_("[--force|--bpb] FILE"),
303 N_("Load another boot loader."));
307 GRUB_MOD_FINI(chainloader)
309 grub_unregister_command (cmd);