6 #include <syslinux/config.h>
14 static const char cmldr_signature[8] = "cmdcons";
16 /* Create boot info table: needed when you want to chainload
17 * another version of ISOLINUX (or another bootlaoder that needs
18 * the -boot-info-table switch of mkisofs)
19 * (will only work when run from ISOLINUX)
21 int manglef_isolinux(struct data_area *data)
23 const union syslinux_derivative_info *sdi;
24 unsigned char *isolinux_bin;
25 uint32_t *checksum, *chkhead, *chktail;
26 uint32_t file_lba = 0;
28 if (!(opt.file && opt.isolinux))
31 sdi = syslinux_derivative_info();
33 if (sdi->c.filesystem != SYSLINUX_FS_ISOLINUX) {
34 error ("The isolinux= option is only valid when run from ISOLINUX.\n");
38 /* Boot info table info (integers in little endian format)
40 Offset Name Size Meaning
41 8 bi_pvd 4 bytes LBA of primary volume descriptor
42 12 bi_file 4 bytes LBA of boot file
43 16 bi_length 4 bytes Boot file length in bytes
44 20 bi_csum 4 bytes 32-bit checksum
45 24 bi_reserved 40 bytes Reserved
47 The 32-bit checksum is the sum of all the 32-bit words in the
48 boot file starting at byte offset 64. All linear block
49 addresses (LBAs) are given in CD sectors (normally 2048 bytes).
51 LBA of primary volume descriptor should already be set to 16.
54 isolinux_bin = (unsigned char *)data->data;
56 /* Get LBA address of bootfile */
57 file_lba = get_file_lba(opt.file);
60 error("Failed to find LBA offset of the boot file\n");
64 *((uint32_t *) & isolinux_bin[12]) = file_lba;
66 /* Set boot file length */
67 *((uint32_t *) & isolinux_bin[16]) = data->size;
69 /* Calculate checksum */
70 checksum = (uint32_t *) & isolinux_bin[20];
71 chkhead = (uint32_t *) & isolinux_bin[64];
72 chktail = (uint32_t *) & isolinux_bin[data->size & ~3u];
74 while (chkhead < chktail)
75 *checksum += *chkhead++;
78 * Deal with possible fractional dword at the end;
79 * this *should* never happen...
83 memcpy(&xword, chkhead, data->size & 3);
92 * Legacy grub's stage2 chainloading
94 int manglef_grub(const struct part_iter *iter, struct data_area *data)
96 /* Layout of stage2 file (from byte 0x0 to 0x270) */
97 struct grub_stage2_patch_area {
100 /* 0x206: compatibility version number major */
101 uint8_t compat_version_major;
102 /* 0x207: compatibility version number minor */
103 uint8_t compat_version_minor;
105 /* 0x208: install_partition variable */
107 /* 0x208: sub-partition in sub-partition part2 */
109 /* 0x209: sub-partition in top-level partition */
111 /* 0x20a: top-level partiton number */
113 /* 0x20b: BIOS drive number (must be 0) */
115 } __attribute__ ((packed)) install_partition;
117 /* 0x20c: deprecated (historical reason only) */
118 uint32_t saved_entryno;
119 /* 0x210: stage2_ID: will always be STAGE2_ID_STAGE2 = 0 in stage2 */
121 /* 0x211: force LBA */
123 /* 0x212: version string (will probably be 0.97) */
124 char version_string[5];
125 /* 0x217: config filename */
126 char config_file[89];
127 /* 0x270: start of code (after jump from 0x200) */
129 } __attribute__ ((packed)) *stage2;
131 if (!(opt.file && opt.grub))
134 if (data->size < sizeof(struct grub_stage2_patch_area)) {
135 error("The file specified by grub=<loader> is too small to be stage2 of GRUB Legacy.\n");
141 * Check the compatibility version number to see if we loaded a real
142 * stage2 file or a stage2 file that we support.
144 if (stage2->compat_version_major != 3
145 || stage2->compat_version_minor != 2) {
146 error("The file specified by grub=<loader> is not a supported stage2 GRUB Legacy binary.\n");
151 * GRUB Legacy wants the partition number in the install_partition
152 * variable, located at offset 0x208 of stage2.
153 * When GRUB Legacy is loaded, it is located at memory address 0x8208.
155 * It looks very similar to the "boot information format" of the
156 * Multiboot specification:
157 * http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format
159 * 0x208 = part3: sub-partition in sub-partition part2
160 * 0x209 = part2: sub-partition in top-level partition
161 * 0x20a = part1: top-level partition number
162 * 0x20b = drive: BIOS drive number (must be 0)
164 * GRUB Legacy doesn't store the BIOS drive number at 0x20b, but at
167 * Partition numbers always start from zero.
168 * Unused partition bytes must be set to 0xFF.
170 * We only care about top-level partition, so we only need to change
171 * "part1" to the appropriate value:
172 * -1: whole drive (default) (-1 = 0xFF)
173 * 0-3: primary partitions
174 * 4-*: logical partitions
176 stage2->install_partition.part1 = (uint8_t)(iter->index - 1);
179 * Grub Legacy reserves 89 bytes (from 0x8217 to 0x826f) for the
180 * config filename. The filename passed via grubcfg= will overwrite
181 * the default config filename "/boot/grub/menu.lst".
184 if (strlen(opt.grubcfg) > sizeof(stage2->config_file) - 1) {
185 error ("The config filename length can't exceed 88 characters.\n");
189 strcpy((char *)stage2->config_file, opt.grubcfg);
198 * Dell's DRMK chainloading.
200 int manglef_drmk(struct data_area *data)
203 * DRMK entry is different than MS-DOS/PC-DOS
204 * A new size, aligned to 16 bytes to ease use of ds:[bp+28].
205 * We only really need 4 new, usable bytes at the end.
208 if (!(opt.file && opt.drmk))
211 uint32_t tsize = (data->size + 19) & 0xfffffff0;
212 opt.regs.ss = opt.regs.fs = opt.regs.gs = 0; /* Used before initialized */
213 if (!realloc(data->data, tsize)) {
214 error("Failed to realloc for DRMK.\n");
218 /* ds:[bp+28] must be 0x0000003f */
219 opt.regs.ds = (uint16_t)((tsize >> 4) + (opt.fseg - 2));
220 /* "Patch" into tail of the new space */
221 *(uint32_t *)((char*)data->data + tsize - 4) = 0x0000003f;
228 /* Adjust BPB common function */
229 static int mangle_bpb(const struct part_iter *iter, struct data_area *data)
232 int type = bpb_detect(data->data);
234 /* BPB: hidden sectors 32bit*/
235 if (type >= bpbV34) {
236 if (iter->start_lba < ~0u)
237 *(uint32_t *) ((char *)data->data + 0x1c) = (uint32_t)iter->start_lba;
239 /* won't really help much, but ... */
240 *(uint32_t *) ((char *)data->data + 0x1c) = ~0u;
242 /* BPB: hidden sectors 16bit*/
243 if (bpbV30 <= type && type <= bpbV32) {
244 if (iter->start_lba < 0xFFFF)
245 *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)iter->start_lba;
247 /* won't really help much, but ... */
248 *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)~0u;
250 /* BPB: legacy geometry */
251 if (type >= bpbV30) {
253 *(uint32_t *)((char *)data->data + 0x18) = (uint32_t)((iter->di.head << 16) | iter->di.sect);
255 if (iter->di.disk & 0x80)
256 *(uint32_t *)((char *)data->data + 0x18) = 0x00FF003F;
258 *(uint32_t *)((char *)data->data + 0x18) = 0x00020012;
262 if (drvoff_detect(type, &off)) {
263 *(uint8_t *)((char *)data->data + off) = (uint8_t)
264 (opt.swap ? iter->di.disk & 0x80 : iter->di.disk);
271 * Adjust BPB of a BPB-compatible file
273 int manglef_bpb(const struct part_iter *iter, struct data_area *data)
275 if (!(opt.file && opt.filebpb))
278 return mangle_bpb(iter, data);
282 * Adjust BPB of a sector
284 int mangles_bpb(const struct part_iter *iter, struct data_area *data)
286 if (!(opt.sect && opt.setbpb))
289 return mangle_bpb(iter, data);
293 * This function performs full BPB patching, analogously to syslinux's
294 * native BSS. opt.drv is prereq
296 int manglesf_bss(struct data_area *sec, struct data_area *fil)
299 unsigned int cnt = 0;
301 if (!(opt.sect && opt.file && opt.bss))
304 type1 = bpb_detect(fil->data);
305 type2 = bpb_detect(sec->data);
307 if (type1 < 0 || type2 < 0) {
308 error("Option 'bss' can't determine BPB type.\n");
311 if (type1 != type2) {
312 error("Option 'bss' can't be used,\n"
313 "when a sector and a file have incompatible BPBs.\n");
317 /* Copy common 2.0 data */
318 memcpy((char *)fil->data + 0x0B, (char *)sec->data + 0x0B, 0x0D);
321 if (type1 <= bpbV30) {
323 } else if (type1 <= bpbV32) {
325 } else if (type1 <= bpbV34) {
327 } else if (type1 <= bpbV40) {
329 } else if (type1 <= bpbVNT) {
331 } else if (type1 <= bpbV70) {
334 memcpy((char *)fil->data + 0x18, (char *)sec->data + 0x18, cnt);
344 int mangles_save(const struct part_iter *iter, const struct data_area *data, void *org)
346 if (!(opt.sect && opt.save))
349 if (memcmp(org, data->data, data->size)) {
350 if (disk_write_sector(&iter->di, iter->start_lba, data->data)) {
351 error("Cannot write the updated sector.\n");
354 /* function can be called again */
355 memcpy(org, data->data, data->size);
364 * To boot the Recovery Console of Windows NT/2K/XP we need to write
365 * the string "cmdcons\0" to memory location 0000:7C03.
366 * Memory location 0000:7C00 contains the bootsector of the partition.
368 int mangles_cmldr(struct data_area *data)
370 if (!(opt.sect && opt.cmldr))
373 memcpy((char *)data->data + 3, cmldr_signature, sizeof(cmldr_signature));
377 /* Set common registers */
378 int mangler_common(const struct part_iter *iter)
380 /* Set initial registry values */
382 opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.fseg;
383 opt.regs.ip = (uint16_t)opt.fip;
385 opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.sseg;
386 opt.regs.ip = (uint16_t)opt.sip;
389 if (opt.regs.ip == 0x7C00 && !opt.regs.cs)
390 opt.regs.esp.l = 0x7C00;
392 /* DOS kernels want the drive number in BL instead of DL. Indulge them. */
393 opt.regs.ebx.b[0] = opt.regs.edx.b[0] = (uint8_t)iter->di.disk;
399 int mangler_handover(const struct part_iter *iter, const struct data_area *data)
401 if (opt.sect && opt.file && opt.maps && !opt.hptr) {
402 opt.regs.esi.l = opt.regs.ebp.l = opt.soff;
403 opt.regs.ds = (uint16_t)opt.sseg;
405 } else if (opt.hand) {
406 /* base is really 0x7be */
407 opt.regs.esi.l = opt.regs.ebp.l = data->base;
409 if (iter->type == typegpt)
410 opt.regs.eax.l = 0x54504721; /* '!GPT' */
419 * GRLDR of GRUB4DOS wants the partition number in DH:
420 * -1: whole drive (default)
421 * 0-3: primary partitions
422 * 4-*: logical partitions
424 int mangler_grldr(const struct part_iter *iter)
427 opt.regs.edx.b[1] = (uint8_t)(iter->index - 1);
432 /* vim: set ts=8 sts=4 sw=4 noet: */