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 const union syslinux_derivative_info *sdi;
215 sdi = syslinux_derivative_info();
216 /* We should lookup the Syslinux partition offset and use it */
217 fs_lba = *sdi->disk.partoffset;
220 * fs_lba should be verified against the disk as some DRMK
221 * variants will check and fail if it does not match
223 dprintf(" fs_lba offset is %d\n", fs_lba);
224 /* DRMK only uses a DWORD */
225 if (fs_lba > 0xffffffff) {
226 error("LBA very large; Only using lower 32 bits; DRMK will probably fail\n");
228 opt.regs.ss = opt.regs.fs = opt.regs.gs = 0; /* Used before initialized */
229 if (!realloc(data->data, tsize)) {
230 error("Failed to realloc for DRMK.\n");
234 /* ds:bp is assumed by DRMK to be the boot sector */
235 /* offset 28 is the FAT HiddenSectors value */
236 opt.regs.ds = (uint16_t)((tsize >> 4) + (opt.fseg - 2));
237 /* "Patch" into tail of the new space */
238 *(uint32_t *)((char*)data->data + tsize - 4) = (uint32_t)fs_lba;
245 /* Adjust BPB common function */
246 static int mangle_bpb(const struct part_iter *iter, struct data_area *data, const char *tag)
249 int type = bpb_detect(data->data, tag);
251 /* BPB: hidden sectors 32bit*/
252 if (type >= bpbV34) {
253 if (iter->start_lba < ~0u)
254 *(uint32_t *) ((char *)data->data + 0x1c) = (uint32_t)iter->start_lba;
256 /* won't really help much, but ... */
257 *(uint32_t *) ((char *)data->data + 0x1c) = ~0u;
259 /* BPB: hidden sectors 16bit*/
260 if (bpbV30 <= type && type <= bpbV32) {
261 if (iter->start_lba < 0xFFFF)
262 *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)iter->start_lba;
264 /* won't really help much, but ... */
265 *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)~0u;
267 /* BPB: legacy geometry */
268 if (type >= bpbV30) {
270 *(uint32_t *)((char *)data->data + 0x18) = (uint32_t)((iter->di.head << 16) | iter->di.spt);
272 if (iter->di.disk & 0x80)
273 *(uint32_t *)((char *)data->data + 0x18) = 0x00FF003F;
275 *(uint32_t *)((char *)data->data + 0x18) = 0x00020012;
279 if (drvoff_detect(type, &off)) {
280 *(uint8_t *)((char *)data->data + off) = (uint8_t)
281 (opt.swap ? iter->di.disk & 0x80 : iter->di.disk);
288 * Adjust BPB of a BPB-compatible file
290 int manglef_bpb(const struct part_iter *iter, struct data_area *data)
292 if (!(opt.file && opt.filebpb))
295 return mangle_bpb(iter, data, "file");
299 * Adjust BPB of a sector
301 int mangles_bpb(const struct part_iter *iter, struct data_area *data)
303 if (!(opt.sect && opt.setbpb))
306 return mangle_bpb(iter, data, "sect");
310 * This function performs full BPB patching, analogously to syslinux's
313 int manglesf_bss(struct data_area *sec, struct data_area *fil)
316 unsigned int cnt = 0;
318 if (!(opt.sect && opt.file && opt.bss))
321 type1 = bpb_detect(fil->data, "bss/file");
322 type2 = bpb_detect(sec->data, "bss/sect");
324 if (!type1 || !type2) {
325 error("Couldn't determine the BPB type for option 'bss'.\n");
328 if (type1 != type2) {
329 error("Option 'bss' can't be used,\n"
330 "when a sector and a file have incompatible BPBs.\n");
334 /* Copy common 2.0 data */
335 memcpy((char *)fil->data + 0x0B, (char *)sec->data + 0x0B, 0x0D);
338 if (type1 <= bpbV30) {
340 } else if (type1 <= bpbV32) {
342 } else if (type1 <= bpbV34) {
344 } else if (type1 <= bpbV40) {
346 } else if (type1 <= bpbVNT) {
348 } else if (type1 <= bpbV70) {
351 memcpy((char *)fil->data + 0x18, (char *)sec->data + 0x18, cnt);
361 int mangles_save(const struct part_iter *iter, const struct data_area *data, void *org)
363 if (!(opt.sect && opt.save))
366 if (memcmp(org, data->data, data->size)) {
367 if (disk_write_sectors(&iter->di, iter->start_lba, data->data, 1)) {
368 error("Cannot write the updated sector.\n");
371 /* function can be called again */
372 memcpy(org, data->data, data->size);
381 * To boot the Recovery Console of Windows NT/2K/XP we need to write
382 * the string "cmdcons\0" to memory location 0000:7C03.
383 * Memory location 0000:7C00 contains the bootsector of the partition.
385 int mangles_cmldr(struct data_area *data)
387 if (!(opt.sect && opt.cmldr))
390 memcpy((char *)data->data + 3, cmldr_signature, sizeof(cmldr_signature));
394 /* Set common registers */
395 int mangler_init(const struct part_iter *iter)
397 /* Set initial registry values */
399 opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.fseg;
400 opt.regs.ip = (uint16_t)opt.fip;
402 opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.sseg;
403 opt.regs.ip = (uint16_t)opt.sip;
406 if (opt.regs.ip == 0x7C00 && !opt.regs.cs)
407 opt.regs.esp.l = 0x7C00;
409 /* DOS kernels want the drive number in BL instead of DL. Indulge them. */
410 opt.regs.ebx.b[0] = opt.regs.edx.b[0] = (uint8_t)iter->di.disk;
416 int mangler_handover(const struct part_iter *iter, const struct data_area *data)
418 if (opt.file && opt.maps && !opt.hptr) {
419 opt.regs.esi.l = opt.regs.ebp.l = opt.soff;
420 opt.regs.ds = (uint16_t)opt.sseg;
422 } else if (opt.hand) {
423 /* base is really 0x7be */
424 opt.regs.esi.l = opt.regs.ebp.l = data->base;
426 if (iter->index && iter->type == typegpt) /* must be iterated and GPT */
427 opt.regs.eax.l = 0x54504721; /* '!GPT' */
436 * GRLDR of GRUB4DOS wants the partition number in DH:
437 * -1: whole drive (default)
438 * 0-3: primary partitions
439 * 4-*: logical partitions
441 int mangler_grldr(const struct part_iter *iter)
444 opt.regs.edx.b[1] = (uint8_t)(iter->index - 1);
450 * try to copy values from temporary iterator, if positions match
452 static void push_embr(struct part_iter *diter, struct part_iter *siter)
454 if (diter->sub.dos.cebr_lba == siter->sub.dos.cebr_lba &&
455 diter->di.disk == siter->di.disk) {
456 memcpy(diter->data, siter->data, sizeof(struct disk_dos_mbr));
460 static int mpe_sethide(struct part_iter *iter, struct part_iter *miter)
462 struct disk_dos_part_entry *dp;
463 static const uint16_t mask =
464 (1 << 0x01) | (1 << 0x04) | (1 << 0x06) |
465 (1 << 0x07) | (1 << 0x0b) | (1 << 0x0c) | (1 << 0x0e);
468 dp = (struct disk_dos_part_entry *)iter->record;
471 if ((t <= 0x1f) && ((mask >> (t & ~0x10u)) & 1)) {
472 /* It's a hideable partition type */
473 if (miter->index == iter->index || opt.hide & 4)
474 t &= (uint8_t)(~0x10u); /* unhide */
476 t |= 0x10u; /* hide */
478 if (dp->ostype != t) {
486 * miter - iterator we match against
488 * ..| - enable (1) / disable (0)
489 * .|. - all (1) / pri (0)
490 * |.. - unhide (1) / hide (0)
492 int manglepe_hide(struct part_iter *miter)
494 int wb = 0, werr = 0;
495 struct part_iter *iter = NULL;
496 struct disk_dos_part_entry *dp;
502 if (miter->type != typedos) {
503 error("Options '*hide*' is meaningful only for legacy partition scheme.\n");
507 if (miter->index < 1)
508 error("WARNING: It's impossible to unhide a disk.\n");
510 if (miter->index > 4 && !(opt.hide & 2))
511 error("WARNING: your partition is beyond mbr, so it can't be unhidden without '*hideall'.\n");
513 if (!(iter = pi_begin(&miter->di, 1))) /* turn stepall on */
516 while (!pi_next(&iter) && !werr) {
517 ridx = iter->rawindex;
518 if (!(opt.hide & 2) && ridx > 4)
519 break; /* skip when we're constrained to pri only */
521 dp = (struct disk_dos_part_entry *)iter->record;
523 wb |= mpe_sethide(iter, miter);
525 if (ridx >= 4 && wb && !werr) {
526 push_embr(miter, iter);
527 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
532 if (iter->status > PI_DONE)
537 push_embr(miter, iter);
538 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
541 error("WARNING: failed to write E/MBR during '*hide*'\n");
548 static int mpe_setchs(const struct disk_info *di,
549 struct disk_dos_part_entry *dp,
552 uint32_t ochs1, ochs2;
554 ochs1 = *(uint32_t *)dp->start;
555 ochs2 = *(uint32_t *)dp->end;
557 lba2chs(&dp->start, di, lba1, l2c_cadd);
558 lba2chs(&dp->end, di, lba1 + dp->length - 1, l2c_cadd);
561 *(uint32_t *)dp->start != ochs1 ||
562 *(uint32_t *)dp->end != ochs2;
566 * miter - iterator we match against
568 int manglepe_fixchs(struct part_iter *miter)
570 int wb = 0, werr = 0;
571 struct part_iter *iter = NULL;
572 struct disk_dos_part_entry *dp;
578 if (miter->type != typedos) {
579 error("Options 'fixchs' is meaningful only for legacy partition scheme.\n");
583 if (!(iter = pi_begin(&miter->di, 1))) /* turn stepall on */
586 while (!pi_next(&iter) && !werr) {
587 ridx = iter->rawindex;
588 dp = (struct disk_dos_part_entry *)iter->record;
590 wb |= mpe_setchs(&iter->di, dp, (uint32_t)iter->start_lba);
592 wb |= mpe_setchs(&iter->di, dp + 1, iter->sub.dos.nebr_lba);
594 if (ridx >= 4 && wb && !werr) {
595 push_embr(miter, iter);
596 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
601 if (iter->status > PI_DONE)
606 push_embr(miter, iter);
607 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
610 error("WARNING: failed to write E/MBR during 'fixchs'\n");
617 /* vim: set ts=8 sts=4 sw=4 noet: */