dos: Work-in-progress
[hdt-cyring.git] / dos / syslinux.c
blob891980e8e6082445d83f5370d981d54148effad6
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
6 * This program 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, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
15 * syslinux.c - Linux installer program for SYSLINUX
17 * Hacked up for DOS.
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 //#include <stdarg.h>
26 #include "mystuff.h"
28 #include "syslinux.h"
29 #include "libfat.h"
30 #include "setadv.h"
31 #include "sysexits.h"
32 #include "syslxopt.h"
34 const char *program = "syslinux"; /* Name of program */
35 uint16_t dos_version;
37 #ifdef DEBUG
38 # define dprintf printf
39 void pause(void)
41 uint16_t ax;
43 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
45 #else
46 # define dprintf(...) ((void)0)
47 # define pause() ((void)0)
48 #endif
50 void unlock_device(int);
52 void __attribute__ ((noreturn)) die(const char *msg)
54 unlock_device(0);
55 puts("syslinux: ");
56 puts(msg);
57 putchar('\n');
58 exit(1);
61 void warning(const char *msg)
63 puts("syslinux: warning: ");
64 puts(msg);
65 putchar('\n');
69 * read/write wrapper functions
71 int creat(const char *filename, int mode)
73 uint16_t rv;
74 uint8_t err;
76 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
78 rv = 0x3C00;
79 asm volatile ("int $0x21 ; setc %0"
80 : "=bcdm" (err), "+a" (rv)
81 : "c" (mode), "d" (filename));
82 if (err) {
83 dprintf("rv = %d\n", rv);
84 die("cannot open ldlinux.sys");
87 return rv;
90 void close(int fd)
92 uint16_t rv = 0x3E00;
94 dprintf("close(%d)\n", fd);
96 asm volatile ("int $0x21":"+a" (rv)
97 :"b"(fd));
99 /* The only error MS-DOS returns for close is EBADF,
100 and we really don't care... */
103 int rename(const char *oldname, const char *newname)
105 uint16_t rv = 0x5600; /* Also support 43FFh? */
106 uint8_t err;
108 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
110 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
111 :"d"(oldname), "D"(newname));
113 if (err) {
114 dprintf("rv = %d\n", rv);
115 warning("cannot move ldlinux.sys");
116 return rv;
119 return 0;
122 extern const char __payload_sseg[];
123 uint16_t ldlinux_seg;
125 ssize_t write_ldlinux(int fd)
127 uint32_t offset = 0;
128 uint16_t rv;
129 uint8_t err;
131 while (offset < syslinux_ldlinux_len) {
132 uint32_t chunk = syslinux_ldlinux_len - offset;
133 if (chunk > 32768)
134 chunk = 32768;
135 asm volatile ("pushw %%ds ; "
136 "movw %6,%%ds ; "
137 "int $0x21 ; "
138 "popw %%ds ; " "setc %0":"=bcdm" (err), "=a"(rv)
139 :"a"(0x4000), "b"(fd), "c"(chunk), "d"(offset & 15),
140 "SD"((uint16_t) (ldlinux_seg + (offset >> 4))));
141 if (err || rv == 0)
142 die("file write error");
143 offset += rv;
146 return offset;
149 ssize_t write_file(int fd, const void *buf, size_t count)
151 uint16_t rv;
152 ssize_t done = 0;
153 uint8_t err;
155 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
157 while (count) {
158 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv)
159 :"a"(0x4000), "b"(fd), "c"(count), "d"(buf));
160 if (err || rv == 0)
161 die("file write error");
163 done += rv;
164 count -= rv;
167 return done;
170 static inline __attribute__ ((const))
171 uint16_t data_segment(void)
173 uint16_t ds;
175 asm("movw %%ds,%0" : "=rm"(ds));
176 return ds;
179 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
181 uint16_t errnum;
182 struct diskio dio;
184 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
186 dio.startsector = sector;
187 dio.sectors = nsecs;
188 dio.bufoffs = (uintptr_t) buf;
189 dio.bufseg = data_segment();
191 /* Try FAT32-aware system call first */
192 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
193 "1:"
194 : "=a" (errnum)
195 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
196 "S" (1), "m" (dio)
197 : "memory");
199 /* If not supported, try the legacy system call (int2526.S) */
200 if (errnum == 0x0001)
201 errnum = int26_write_sector(drive, &dio);
203 if (errnum) {
204 dprintf("rv = %04x\n", errnum);
205 die("sector write error");
209 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
211 uint16_t errnum;
212 struct diskio dio;
214 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
216 dio.startsector = sector;
217 dio.sectors = nsecs;
218 dio.bufoffs = (uintptr_t) buf;
219 dio.bufseg = data_segment();
221 /* Try FAT32-aware system call first */
222 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
223 "1:"
224 : "=a" (errnum)
225 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
226 "S" (0), "m" (dio));
228 /* If not supported, try the legacy system call (int2526.S) */
229 if (errnum == 0x0001)
230 errnum = int25_read_sector(drive, &dio);
232 if (errnum) {
233 dprintf("rv = %04x\n", errnum);
234 die("sector read error");
238 /* Both traditional DOS and FAT32 DOS return this structure, but
239 FAT32 return a lot more data, so make sure we have plenty of space */
240 struct deviceparams {
241 uint8_t specfunc;
242 uint8_t devtype;
243 uint16_t devattr;
244 uint16_t cylinders;
245 uint8_t mediatype;
246 uint16_t bytespersec;
247 uint8_t secperclust;
248 uint16_t ressectors;
249 uint8_t fats;
250 uint16_t rootdirents;
251 uint16_t sectors;
252 uint8_t media;
253 uint16_t fatsecs;
254 uint16_t secpertrack;
255 uint16_t heads;
256 uint32_t hiddensecs;
257 uint32_t hugesectors;
258 uint8_t lotsofpadding[224];
259 } __attribute__ ((packed));
261 uint32_t get_partition_offset(int drive)
263 uint8_t err;
264 uint16_t rv;
265 struct deviceparams dp;
267 dp.specfunc = 1; /* Get current information */
269 rv = 0x440d;
270 asm volatile ("int $0x21 ; setc %0"
271 :"=abcdm" (err), "+a"(rv), "=m"(dp)
272 :"b" (drive), "c" (0x0860), "d" (&dp));
274 if (!err)
275 return dp.hiddensecs;
277 rv = 0x440d;
278 asm volatile ("int $0x21 ; setc %0"
279 : "=abcdm" (err), "+a" (rv), "=m" (dp)
280 : "b" (drive), "c" (0x4860), "d" (&dp));
282 if (!err)
283 return dp.hiddensecs;
285 die("could not find partition start offset");
288 struct rwblock {
289 uint8_t special;
290 uint16_t head;
291 uint16_t cylinder;
292 uint16_t firstsector;
293 uint16_t sectors;
294 uint16_t bufferoffset;
295 uint16_t bufferseg;
296 } __attribute__ ((packed));
298 static struct rwblock mbr = {
299 .special = 0,
300 .head = 0,
301 .cylinder = 0,
302 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
303 .sectors = 1,
304 .bufferoffset = 0,
305 .bufferseg = 0
308 void write_mbr(int drive, const void *buf)
310 uint16_t rv;
311 uint8_t err;
313 dprintf("write_mbr(%d,%p)", drive, buf);
315 mbr.bufferoffset = (uintptr_t) buf;
316 mbr.bufferseg = data_segment();
318 rv = 0x440d;
319 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
320 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
322 dprintf(" rv(0841) = %04x", rv);
323 if (!err) {
324 dprintf("\n");
325 return;
328 rv = 0x440d;
329 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
330 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
332 dprintf(" rv(4841) = %04x\n", rv);
333 if (err)
334 die("mbr write error");
337 void read_mbr(int drive, const void *buf)
339 uint16_t rv;
340 uint8_t err;
342 dprintf("read_mbr(%d,%p)", drive, buf);
344 mbr.bufferoffset = (uintptr_t) buf;
345 mbr.bufferseg = data_segment();
347 rv = 0x440d;
348 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
349 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
351 dprintf(" rv(0861) = %04x", rv);
352 if (!err) {
353 dprintf("\n");
354 return;
357 rv = 0x440d;
358 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
359 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
361 dprintf(" rv(4841) = %04x\n", rv);
362 if (err)
363 die("mbr read error");
365 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
366 ((const uint8_t *)buf)[0],
367 ((const uint8_t *)buf)[1],
368 ((const uint8_t *)buf)[2],
369 ((const uint8_t *)buf)[3],
370 ((const uint8_t *)buf)[4],
371 ((const uint8_t *)buf)[5],
372 ((const uint8_t *)buf)[6],
373 ((const uint8_t *)buf)[7]);
376 /* This call can legitimately fail, and we don't care, so ignore error return */
377 void set_attributes(const char *file, int attributes)
379 uint16_t rv = 0x4301;
381 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
383 asm volatile ("int $0x21":"+a" (rv)
384 :"c"(attributes), "d"(file));
388 * Version of the read_device function suitable for libfat
390 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
391 libfat_sector_t sector)
393 read_device(pp, buf, 1, sector);
394 return secsize;
397 static inline void get_dos_version(void)
399 uint16_t ver;
401 asm("int $0x21 ; xchgb %%ah,%%al"
402 : "=a" (ver)
403 : "a" (0x3001)
404 : "ebx", "ecx");
405 dos_version = ver;
407 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
410 /* The locking interface relies on static variables. A massive hack :( */
411 static uint8_t lock_level, lock_drive;
413 static inline void set_lock_device(uint8_t device)
415 lock_level = 0;
416 lock_drive = device;
419 static int do_lock(uint8_t level)
421 uint16_t level_arg = lock_drive + (level << 8);
422 uint16_t rv;
423 uint8_t err;
424 #if 0
425 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
426 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
427 #else
428 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
429 #endif
431 dprintf("Trying lock %04x... ", level_arg);
432 asm volatile ("int $0x21 ; setc %0"
433 : "=bcdm" (err), "=a" (rv)
434 : "a" (0x440d), "b" (level_arg),
435 "c" (lock_call), "d" (0x0001));
436 dprintf("%s %04x\n", err ? "err" : "ok", rv);
438 return err ? rv : 0;
441 void lock_device(int level)
443 static int hard_lock = 0;
444 int err;
446 if (dos_version < 0x0700)
447 return; /* Win9x/NT only */
449 if (!hard_lock) {
450 /* Assume hierarchial "soft" locking supported */
452 while (lock_level < level) {
453 int new_level = lock_level + 1;
454 err = do_lock(new_level);
455 if (err) {
456 if (err == 0x0001) {
457 /* Try hard locking next */
458 hard_lock = 1;
460 goto soft_fail;
463 lock_level = new_level;
465 return;
468 soft_fail:
469 if (hard_lock) {
470 /* Hard locking, only level 4 supported */
471 /* This is needed for Win9x in DOS mode */
473 err = do_lock(4);
474 if (err) {
475 if (err == 0x0001) {
476 /* Assume locking is not needed */
477 return;
479 goto hard_fail;
482 lock_level = 4;
483 return;
486 hard_fail:
487 die("could not lock device");
490 void unlock_device(int level)
492 uint16_t rv;
493 uint8_t err;
494 uint16_t unlock_call;
496 if (dos_version < 0x0700)
497 return; /* Win9x/NT only */
499 #if 0
500 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
501 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
502 #else
503 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
504 #endif
506 if (lock_level == 4 && level > 0)
507 return; /* Only drop the hard lock at the end */
509 while (lock_level > level) {
510 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
511 uint16_t level_arg = (new_level << 8) + lock_drive;
512 rv = 0x440d;
513 dprintf("Trying unlock %04x... ", new_level);
514 asm volatile ("int $0x21 ; setc %0"
515 : "=bcdm" (err), "+a" (rv)
516 : "b" (level_arg), "c" (unlock_call));
517 dprintf("%s %04x\n", err ? "err" : "ok", rv);
518 lock_level = new_level;
523 * This function does any desired MBR manipulation; called with the device lock held.
525 struct mbr_entry {
526 uint8_t active; /* Active flag */
527 uint8_t bhead; /* Begin head */
528 uint8_t bsector; /* Begin sector */
529 uint8_t bcylinder; /* Begin cylinder */
530 uint8_t filesystem; /* Filesystem value */
531 uint8_t ehead; /* End head */
532 uint8_t esector; /* End sector */
533 uint8_t ecylinder; /* End cylinder */
534 uint32_t startlba; /* Start sector LBA */
535 uint32_t sectors; /* Length in sectors */
536 } __attribute__ ((packed));
538 static void adjust_mbr(int device, int writembr, int set_active)
540 static unsigned char sectbuf[SECTOR_SIZE];
541 int i;
543 if (!writembr && !set_active)
544 return; /* Nothing to do */
546 read_mbr(device, sectbuf);
548 if (writembr) {
549 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
550 *(uint16_t *) (sectbuf + 510) = 0xaa55;
553 if (set_active) {
554 uint32_t offset = get_partition_offset(device);
555 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
556 int found = 0;
558 dprintf("Searching for partition offset: %08x\n", offset);
560 for (i = 0; i < 4; i++) {
561 if (me->startlba == offset) {
562 me->active = 0x80;
563 found++;
564 } else {
565 me->active = 0;
567 me++;
570 if (found < 1) {
571 die("partition not found (-a is not implemented for logical partitions)");
572 } else if (found > 1) {
573 die("multiple aliased partitions found");
577 write_mbr(device, sectbuf);
580 int main(int argc, char *argv[])
582 static unsigned char sectbuf[SECTOR_SIZE];
583 int dev_fd, fd;
584 static char ldlinux_name[] = "@:\\ldlinux.sys";
585 char **argp, *opt;
586 int force = 0; /* -f (force) option */
587 struct libfat_filesystem *fs;
588 libfat_sector_t s, *secp;
589 libfat_sector_t *sectors;
590 int ldlinux_sectors;
591 int32_t ldlinux_cluster;
592 int nsectors;
593 const char *device = NULL, *bootsecfile = NULL;
594 const char *errmsg;
595 int i;
596 int writembr = 0; /* -m (write MBR) option */
597 int set_active = 0; /* -a (set partition active) option */
598 const char *subdir = NULL;
599 int stupid = 0;
600 int raid_mode = 0;
601 int patch_sectors;
603 ldlinux_seg = (size_t) __payload_sseg + data_segment();
605 dprintf("argv = %p\n", argv);
606 for (i = 0; i <= argc; i++)
607 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
609 (void)argc; /* Unused */
611 get_dos_version();
613 for (argp = argv + 1; *argp; argp++) {
614 if (**argp == '-') {
615 opt = *argp + 1;
616 if (!*opt)
617 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
619 while (*opt) {
620 switch (*opt) {
621 case 's': /* Use "safe, slow and stupid" code */
622 stupid = 1;
623 break;
624 case 'r': /* RAID mode */
625 raid_mode = 1;
626 break;
627 case 'f': /* Force install */
628 force = 1;
629 break;
630 case 'm': /* Write MBR */
631 writembr = 1;
632 break;
633 case 'a': /* Set partition active */
634 set_active = 1;
635 break;
636 case 'd':
637 if (argp[1])
638 subdir = *++argp;
639 break;
640 default:
641 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
643 opt++;
645 } else {
646 if (bootsecfile)
647 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
648 else if (device)
649 bootsecfile = *argp;
650 else
651 device = *argp;
655 if (!device)
656 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
659 * Create an ADV in memory... this should be smarter.
661 syslinux_reset_adv(syslinux_adv);
664 * Figure out which drive we're talking to
666 dev_fd = (device[0] & ~0x20) - 0x40;
667 if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
668 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
670 set_lock_device(dev_fd);
672 lock_device(2); /* Make sure we can lock the device */
673 read_device(dev_fd, sectbuf, 1, 0);
674 unlock_device(1);
677 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
679 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
680 unlock_device(0);
681 puts(errmsg);
682 putchar('\n');
683 exit(1);
686 ldlinux_name[0] = dev_fd | 0x40;
688 set_attributes(ldlinux_name, 0);
689 fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */
690 write_ldlinux(fd);
691 write_file(fd, syslinux_adv, 2 * ADV_SIZE);
692 close(fd);
693 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
696 * Now, use libfat to create a block map. This probably
697 * should be changed to use ioctl(...,FIBMAP,...) since
698 * this is supposed to be a simple, privileged version
699 * of the installer.
701 ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE
702 + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
703 sectors = calloc(ldlinux_sectors, sizeof *sectors);
704 lock_device(2);
705 fs = libfat_open(libfat_xpread, dev_fd);
706 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
707 secp = sectors;
708 nsectors = 0;
709 s = libfat_clustertosector(fs, ldlinux_cluster);
710 while (s && nsectors < ldlinux_sectors) {
711 *secp++ = s;
712 nsectors++;
713 s = libfat_nextsector(fs, s);
715 libfat_close(fs);
718 * If requested, move ldlinux.sys
720 if (subdir) {
721 char new_ldlinux_name[160];
722 char *cp = new_ldlinux_name + 3;
723 const char *sd;
724 int slash = 1;
726 new_ldlinux_name[0] = dev_fd | 0x40;
727 new_ldlinux_name[1] = ':';
728 new_ldlinux_name[2] = '\\';
730 for (sd = subdir; *sd; sd++) {
731 char c = *sd;
733 if (c == '/' || c == '\\') {
734 if (slash)
735 continue;
736 c = '\\';
737 slash = 1;
738 } else {
739 slash = 0;
742 *cp++ = c;
745 /* Skip if subdirectory == root */
746 if (cp > new_ldlinux_name + 3) {
747 if (!slash)
748 *cp++ = '\\';
750 memcpy(cp, "ldlinux.sys", 12);
752 set_attributes(ldlinux_name, 0);
753 if (rename(ldlinux_name, new_ldlinux_name))
754 set_attributes(ldlinux_name, 0x07);
755 else
756 set_attributes(new_ldlinux_name, 0x07);
761 * Patch ldlinux.sys and the boot sector
763 i = syslinux_patch(sectors, nsectors, stupid, raid_mode, subdir, NULL);
764 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
767 * Overwrite the now-patched ldlinux.sys
769 /* lock_device(3); -- doesn't seem to be needed */
770 for (i = 0; i < patch_sectors; i++) {
771 uint16_t si, di, cx;
772 si = 0;
773 di = (size_t) sectbuf;
774 cx = SECTOR_SIZE >> 2;
775 asm volatile ("movw %3,%%fs ; fs ; rep ; movsl":"+S" (si), "+D"(di),
776 "+c"(cx)
777 :"abd"((uint16_t)
778 (ldlinux_seg + (i << (SECTOR_SHIFT - 4)))));
779 write_device(dev_fd, sectbuf, 1, sectors[i]);
783 * Muck with the MBR, if desired, while we hold the lock
785 adjust_mbr(dev_fd, writembr, set_active);
788 * To finish up, write the boot sector
791 /* Read the superblock again since it might have changed while mounted */
792 read_device(dev_fd, sectbuf, 1, 0);
794 /* Copy the syslinux code into the boot sector */
795 syslinux_make_bootsect(sectbuf);
797 /* Write new boot sector */
798 if (bootsecfile) {
799 unlock_device(0);
800 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
801 write_file(fd, sectbuf, SECTOR_SIZE);
802 close(fd);
803 } else {
804 write_device(dev_fd, sectbuf, 1, 0);
805 unlock_device(0);
808 /* Done! */
810 return 0;