Use PRIu64 instead of %llu, where appropriate
[syslinux.git] / com32 / chain / mangle.c
blob8358106e197cba7bc9d0460c46d5de7e7ad7df10
1 #include <com32.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdint.h>
6 #include <dprintf.h>
7 #include <syslinux/config.h>
8 #include "common.h"
9 #include "chain.h"
10 #include "options.h"
11 #include "utility.h"
12 #include "partiter.h"
13 #include "mangle.h"
15 static const char cmldr_signature[8] = "cmdcons";
17 /* Create boot info table: needed when you want to chainload
18 * another version of ISOLINUX (or another bootlaoder that needs
19 * the -boot-info-table switch of mkisofs)
20 * (will only work when run from ISOLINUX)
22 int manglef_isolinux(struct data_area *data)
24 const union syslinux_derivative_info *sdi;
25 unsigned char *isolinux_bin;
26 uint32_t *checksum, *chkhead, *chktail;
27 uint32_t file_lba = 0;
29 if (!(opt.file && opt.isolinux))
30 return 0;
32 sdi = syslinux_derivative_info();
34 if (sdi->c.filesystem != SYSLINUX_FS_ISOLINUX) {
35 error ("The isolinux= option is only valid when run from ISOLINUX.\n");
36 goto bail;
39 /* Boot info table info (integers in little endian format)
41 Offset Name Size Meaning
42 8 bi_pvd 4 bytes LBA of primary volume descriptor
43 12 bi_file 4 bytes LBA of boot file
44 16 bi_length 4 bytes Boot file length in bytes
45 20 bi_csum 4 bytes 32-bit checksum
46 24 bi_reserved 40 bytes Reserved
48 The 32-bit checksum is the sum of all the 32-bit words in the
49 boot file starting at byte offset 64. All linear block
50 addresses (LBAs) are given in CD sectors (normally 2048 bytes).
52 LBA of primary volume descriptor should already be set to 16.
55 isolinux_bin = (unsigned char *)data->data;
57 /* Get LBA address of bootfile */
58 file_lba = get_file_lba(opt.file);
60 if (file_lba == 0) {
61 error("Failed to find LBA offset of the boot file\n");
62 goto bail;
64 /* Set it */
65 *((uint32_t *) & isolinux_bin[12]) = file_lba;
67 /* Set boot file length */
68 *((uint32_t *) & isolinux_bin[16]) = data->size;
70 /* Calculate checksum */
71 checksum = (uint32_t *) & isolinux_bin[20];
72 chkhead = (uint32_t *) & isolinux_bin[64];
73 chktail = (uint32_t *) & isolinux_bin[data->size & ~3u];
74 *checksum = 0;
75 while (chkhead < chktail)
76 *checksum += *chkhead++;
79 * Deal with possible fractional dword at the end;
80 * this *should* never happen...
82 if (data->size & 3) {
83 uint32_t xword = 0;
84 memcpy(&xword, chkhead, data->size & 3);
85 *checksum += xword;
87 return 0;
88 bail:
89 return -1;
93 * Legacy grub's stage2 chainloading
95 int manglef_grub(const struct part_iter *iter, struct data_area *data)
97 /* Layout of stage2 file (from byte 0x0 to 0x270) */
98 struct grub_stage2_patch_area {
99 /* 0x0 to 0x205 */
100 char unknown[0x206];
101 /* 0x206: compatibility version number major */
102 uint8_t compat_version_major;
103 /* 0x207: compatibility version number minor */
104 uint8_t compat_version_minor;
106 /* 0x208: install_partition variable */
107 struct {
108 /* 0x208: sub-partition in sub-partition part2 */
109 uint8_t part3;
110 /* 0x209: sub-partition in top-level partition */
111 uint8_t part2;
112 /* 0x20a: top-level partiton number */
113 uint8_t part1;
114 /* 0x20b: BIOS drive number (must be 0) */
115 uint8_t drive;
116 } __attribute__ ((packed)) install_partition;
118 /* 0x20c: deprecated (historical reason only) */
119 uint32_t saved_entryno;
120 /* 0x210: stage2_ID: will always be STAGE2_ID_STAGE2 = 0 in stage2 */
121 uint8_t stage2_id;
122 /* 0x211: force LBA */
123 uint8_t force_lba;
124 /* 0x212: version string (will probably be 0.97) */
125 char version_string[5];
126 /* 0x217: config filename */
127 char config_file[89];
128 /* 0x270: start of code (after jump from 0x200) */
129 char codestart[1];
130 } __attribute__ ((packed)) *stage2;
132 if (!(opt.file && opt.grub))
133 return 0;
135 if (data->size < sizeof(struct grub_stage2_patch_area)) {
136 error("The file specified by grub=<loader> is too small to be stage2 of GRUB Legacy.\n");
137 goto bail;
139 stage2 = data->data;
142 * Check the compatibility version number to see if we loaded a real
143 * stage2 file or a stage2 file that we support.
145 if (stage2->compat_version_major != 3
146 || stage2->compat_version_minor != 2) {
147 error("The file specified by grub=<loader> is not a supported stage2 GRUB Legacy binary.\n");
148 goto bail;
152 * GRUB Legacy wants the partition number in the install_partition
153 * variable, located at offset 0x208 of stage2.
154 * When GRUB Legacy is loaded, it is located at memory address 0x8208.
156 * It looks very similar to the "boot information format" of the
157 * Multiboot specification:
158 * http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format
160 * 0x208 = part3: sub-partition in sub-partition part2
161 * 0x209 = part2: sub-partition in top-level partition
162 * 0x20a = part1: top-level partition number
163 * 0x20b = drive: BIOS drive number (must be 0)
165 * GRUB Legacy doesn't store the BIOS drive number at 0x20b, but at
166 * another location.
168 * Partition numbers always start from zero.
169 * Unused partition bytes must be set to 0xFF.
171 * We only care about top-level partition, so we only need to change
172 * "part1" to the appropriate value:
173 * -1: whole drive (default) (-1 = 0xFF)
174 * 0-3: primary partitions
175 * 4-*: logical partitions
177 stage2->install_partition.part1 = (uint8_t)(iter->index - 1);
180 * Grub Legacy reserves 89 bytes (from 0x8217 to 0x826f) for the
181 * config filename. The filename passed via grubcfg= will overwrite
182 * the default config filename "/boot/grub/menu.lst".
184 if (opt.grubcfg) {
185 if (strlen(opt.grubcfg) > sizeof(stage2->config_file) - 1) {
186 error ("The config filename length can't exceed 88 characters.\n");
187 goto bail;
190 strcpy((char *)stage2->config_file, opt.grubcfg);
193 return 0;
194 bail:
195 return -1;
197 #if 0
199 * Dell's DRMK chainloading.
201 int manglef_drmk(struct data_area *data)
204 * DRMK entry is different than MS-DOS/PC-DOS
205 * A new size, aligned to 16 bytes to ease use of ds:[bp+28].
206 * We only really need 4 new, usable bytes at the end.
209 if (!(opt.file && opt.drmk))
210 return 0;
212 uint32_t tsize = (data->size + 19) & 0xfffffff0;
213 const union syslinux_derivative_info *sdi;
214 uint64_t fs_lba;
216 sdi = syslinux_derivative_info();
217 /* We should lookup the Syslinux partition offset and use it */
218 fs_lba = *sdi->disk.partoffset;
221 * fs_lba should be verified against the disk as some DRMK
222 * variants will check and fail if it does not match
224 dprintf(" fs_lba offset is %d\n", fs_lba);
225 /* DRMK only uses a DWORD */
226 if (fs_lba > 0xffffffff) {
227 error("LBA very large; Only using lower 32 bits; DRMK will probably fail\n");
229 opt.regs.ss = opt.regs.fs = opt.regs.gs = 0; /* Used before initialized */
230 if (!realloc(data->data, tsize)) {
231 error("Failed to realloc for DRMK.\n");
232 goto bail;
234 data->size = tsize;
235 /* ds:bp is assumed by DRMK to be the boot sector */
236 /* offset 28 is the FAT HiddenSectors value */
237 opt.regs.ds = (uint16_t)((tsize >> 4) + (opt.fseg - 2));
238 /* "Patch" into tail of the new space */
239 *(uint32_t *)((char*)data->data + tsize - 4) = (uint32_t)fs_lba;
241 return 0;
242 bail:
243 return -1;
245 #endif
246 /* Adjust BPB common function */
247 static int mangle_bpb(const struct part_iter *iter, struct data_area *data, const char *tag)
249 unsigned int off;
250 int type = bpb_detect(data->data, tag);
252 /* BPB: hidden sectors 32bit*/
253 if (type >= bpbV34) {
254 if (iter->start_lba < ~0u)
255 *(uint32_t *) ((char *)data->data + 0x1c) = (uint32_t)iter->start_lba;
256 else
257 /* won't really help much, but ... */
258 *(uint32_t *) ((char *)data->data + 0x1c) = ~0u;
260 /* BPB: hidden sectors 16bit*/
261 if (bpbV30 <= type && type <= bpbV32) {
262 if (iter->start_lba < 0xFFFF)
263 *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)iter->start_lba;
264 else
265 /* won't really help much, but ... */
266 *(uint16_t *) ((char *)data->data + 0x1c) = (uint16_t)~0u;
268 /* BPB: legacy geometry */
269 if (type >= bpbV30) {
270 if (iter->di.cbios)
271 *(uint32_t *)((char *)data->data + 0x18) = (uint32_t)((iter->di.head << 16) | iter->di.spt);
272 else {
273 if (iter->di.disk & 0x80)
274 *(uint32_t *)((char *)data->data + 0x18) = 0x00FF003F;
275 else
276 *(uint32_t *)((char *)data->data + 0x18) = 0x00020012;
279 /* BPB: drive */
280 if (drvoff_detect(type, &off)) {
281 *(uint8_t *)((char *)data->data + off) = (uint8_t)
282 (opt.swap ? iter->di.disk & 0x80 : iter->di.disk);
285 return 0;
289 * Adjust BPB of a BPB-compatible file
291 int manglef_bpb(const struct part_iter *iter, struct data_area *data)
293 if (!(opt.file && opt.filebpb))
294 return 0;
296 return mangle_bpb(iter, data, "file");
300 * Adjust BPB of a sector
302 int mangles_bpb(const struct part_iter *iter, struct data_area *data)
304 if (!(opt.sect && opt.setbpb))
305 return 0;
307 return mangle_bpb(iter, data, "sect");
311 * This function performs full BPB patching, analogously to syslinux's
312 * native BSS.
314 int manglesf_bss(struct data_area *sec, struct data_area *fil)
316 int type1, type2;
317 unsigned int cnt = 0;
319 if (!(opt.sect && opt.file && opt.bss))
320 return 0;
322 type1 = bpb_detect(fil->data, "bss/file");
323 type2 = bpb_detect(sec->data, "bss/sect");
325 if (!type1 || !type2) {
326 error("Couldn't determine the BPB type for option 'bss'.\n");
327 goto bail;
329 if (type1 != type2) {
330 error("Option 'bss' can't be used,\n"
331 "when a sector and a file have incompatible BPBs.\n");
332 goto bail;
335 /* Copy common 2.0 data */
336 memcpy((char *)fil->data + 0x0B, (char *)sec->data + 0x0B, 0x0D);
338 /* Copy 3.0+ data */
339 if (type1 <= bpbV30) {
340 cnt = 0x06;
341 } else if (type1 <= bpbV32) {
342 cnt = 0x08;
343 } else if (type1 <= bpbV34) {
344 cnt = 0x0C;
345 } else if (type1 <= bpbV40) {
346 cnt = 0x2E;
347 } else if (type1 <= bpbVNT) {
348 cnt = 0x3C;
349 } else if (type1 <= bpbV70) {
350 cnt = 0x42;
352 memcpy((char *)fil->data + 0x18, (char *)sec->data + 0x18, cnt);
354 return 0;
355 bail:
356 return -1;
360 * Save sector.
362 int mangles_save(const struct part_iter *iter, const struct data_area *data, void *org)
364 if (!(opt.sect && opt.save))
365 return 0;
367 if (memcmp(org, data->data, data->size)) {
368 if (disk_write_sectors(&iter->di, iter->start_lba, data->data, 1)) {
369 error("Cannot write the updated sector.\n");
370 goto bail;
372 /* function can be called again */
373 memcpy(org, data->data, data->size);
376 return 0;
377 bail:
378 return -1;
382 * To boot the Recovery Console of Windows NT/2K/XP we need to write
383 * the string "cmdcons\0" to memory location 0000:7C03.
384 * Memory location 0000:7C00 contains the bootsector of the partition.
386 int mangles_cmldr(struct data_area *data)
388 if (!(opt.sect && opt.cmldr))
389 return 0;
391 memcpy((char *)data->data + 3, cmldr_signature, sizeof(cmldr_signature));
392 return 0;
395 /* Set common registers */
396 int mangler_init(const struct part_iter *iter)
398 /* Set initial registry values */
399 if (opt.file) {
400 opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.fseg;
401 opt.regs.ip = (uint16_t)opt.fip;
402 } else {
403 opt.regs.cs = opt.regs.ds = opt.regs.ss = (uint16_t)opt.sseg;
404 opt.regs.ip = (uint16_t)opt.sip;
407 if (opt.regs.ip == 0x7C00 && !opt.regs.cs)
408 opt.regs.esp.l = 0x7C00;
410 /* DOS kernels want the drive number in BL instead of DL. Indulge them. */
411 opt.regs.ebx.b[0] = opt.regs.edx.b[0] = (uint8_t)iter->di.disk;
413 return 0;
416 /* ds:si & ds:bp */
417 int mangler_handover(const struct part_iter *iter, const struct data_area *data)
419 if (opt.file && opt.maps && !opt.hptr) {
420 opt.regs.esi.l = opt.regs.ebp.l = opt.soff;
421 opt.regs.ds = (uint16_t)opt.sseg;
422 opt.regs.eax.l = 0;
423 } else if (opt.hand) {
424 /* base is really 0x7be */
425 opt.regs.esi.l = opt.regs.ebp.l = data->base;
426 opt.regs.ds = 0;
427 if (iter->index && iter->type == typegpt) /* must be iterated and GPT */
428 opt.regs.eax.l = 0x54504721; /* '!GPT' */
429 else
430 opt.regs.eax.l = 0;
433 return 0;
437 * GRLDR of GRUB4DOS wants the partition number in DH:
438 * -1: whole drive (default)
439 * 0-3: primary partitions
440 * 4-*: logical partitions
442 int mangler_grldr(const struct part_iter *iter)
444 if (opt.grldr)
445 opt.regs.edx.b[1] = (uint8_t)(iter->index - 1);
447 return 0;
451 * try to copy values from temporary iterator, if positions match
453 static void push_embr(struct part_iter *diter, struct part_iter *siter)
455 if (diter->sub.dos.cebr_lba == siter->sub.dos.cebr_lba &&
456 diter->di.disk == siter->di.disk) {
457 memcpy(diter->data, siter->data, sizeof(struct disk_dos_mbr));
461 static int mpe_sethide(struct part_iter *iter, struct part_iter *miter)
463 struct disk_dos_part_entry *dp;
464 static const uint16_t mask =
465 (1 << 0x01) | (1 << 0x04) | (1 << 0x06) |
466 (1 << 0x07) | (1 << 0x0b) | (1 << 0x0c) | (1 << 0x0e);
467 uint8_t t;
469 dp = (struct disk_dos_part_entry *)iter->record;
470 t = dp->ostype;
472 if ((t <= 0x1f) && ((mask >> (t & ~0x10u)) & 1)) {
473 /* It's a hideable partition type */
474 if (miter->index == iter->index || opt.hide & 4)
475 t &= (uint8_t)(~0x10u); /* unhide */
476 else
477 t |= 0x10u; /* hide */
479 if (dp->ostype != t) {
480 dp->ostype = t;
481 return -1;
483 return 0;
487 * miter - iterator we match against
488 * hide bits meaning:
489 * ..| - enable (1) / disable (0)
490 * .|. - all (1) / pri (0)
491 * |.. - unhide (1) / hide (0)
493 int manglepe_hide(struct part_iter *miter)
495 int wb = 0, werr = 0;
496 struct part_iter *iter = NULL;
497 struct disk_dos_part_entry *dp;
498 int ridx;
500 if (!opt.hide)
501 return 0;
503 if (miter->type != typedos) {
504 error("Options '*hide*' is meaningful only for legacy partition scheme.\n");
505 return -1;
508 if (miter->index < 1)
509 error("WARNING: It's impossible to unhide a disk.\n");
511 if (miter->index > 4 && !(opt.hide & 2))
512 error("WARNING: your partition is beyond mbr, so it can't be unhidden without '*hideall'.\n");
514 if (!(iter = pi_begin(&miter->di, 1))) /* turn stepall on */
515 return -1;
517 while (!pi_next(&iter) && !werr) {
518 ridx = iter->rawindex;
519 if (!(opt.hide & 2) && ridx > 4)
520 break; /* skip when we're constrained to pri only */
522 dp = (struct disk_dos_part_entry *)iter->record;
523 if (dp->ostype)
524 wb |= mpe_sethide(iter, miter);
526 if (ridx >= 4 && wb && !werr) {
527 push_embr(miter, iter);
528 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
529 wb = 0;
533 if (iter->status > PI_DONE)
534 goto bail;
536 /* last write */
537 if (wb && !werr) {
538 push_embr(miter, iter);
539 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
541 if (werr)
542 error("WARNING: failed to write E/MBR during '*hide*'\n");
544 bail:
545 pi_del(&iter);
546 return 0;
549 static int mpe_setchs(const struct disk_info *di,
550 struct disk_dos_part_entry *dp,
551 uint32_t lba1)
553 uint32_t ochs1, ochs2;
555 ochs1 = *(uint32_t *)dp->start;
556 ochs2 = *(uint32_t *)dp->end;
558 lba2chs(&dp->start, di, lba1, l2c_cadd);
559 lba2chs(&dp->end, di, lba1 + dp->length - 1, l2c_cadd);
561 return
562 *(uint32_t *)dp->start != ochs1 ||
563 *(uint32_t *)dp->end != ochs2;
567 * miter - iterator we match against
569 int manglepe_fixchs(struct part_iter *miter)
571 int wb = 0, werr = 0;
572 struct part_iter *iter = NULL;
573 struct disk_dos_part_entry *dp;
574 int ridx;
576 if (!opt.fixchs)
577 return 0;
579 if (miter->type != typedos) {
580 error("Options 'fixchs' is meaningful only for legacy partition scheme.\n");
581 return -1;
584 if (!(iter = pi_begin(&miter->di, 1))) /* turn stepall on */
585 return -1;
587 while (!pi_next(&iter) && !werr) {
588 ridx = iter->rawindex;
589 dp = (struct disk_dos_part_entry *)iter->record;
591 wb |= mpe_setchs(&iter->di, dp, (uint32_t)iter->start_lba);
592 if (ridx > 4)
593 wb |= mpe_setchs(&iter->di, dp + 1, iter->sub.dos.nebr_lba);
595 if (ridx >= 4 && wb && !werr) {
596 push_embr(miter, iter);
597 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
598 wb = 0;
602 if (iter->status > PI_DONE)
603 goto bail;
605 /* last write */
606 if (wb && !werr) {
607 push_embr(miter, iter);
608 werr |= disk_write_sectors(&iter->di, iter->sub.dos.cebr_lba, iter->data, 1);
610 if (werr)
611 error("WARNING: failed to write E/MBR during 'fixchs'\n");
613 bail:
614 pi_del(&iter);
615 return 0;
618 /* vim: set ts=8 sts=4 sw=4 noet: */