core/legacynet: Enable dot quad resolution
[syslinux.git] / dos / syslinux.c
blob63a3a854e7c115d2c29efb7c67fe762405a070c5
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 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"
33 #include "syslxint.h"
34 #include "syslxfs.h"
36 char *program = "syslinux.com"; /* Name of program */
37 uint16_t dos_version;
39 #ifdef DEBUG
40 # define dprintf printf
41 void pause(void)
43 uint16_t ax;
45 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
47 #else
48 # define dprintf(...) ((void)0)
49 # define pause() ((void)0)
50 #endif
52 void unlock_device(int);
54 void __attribute__ ((noreturn)) die(const char *msg)
56 unlock_device(0);
57 puts("syslinux: ");
58 puts(msg);
59 putchar('\n');
60 exit(1);
63 void warning(const char *msg)
65 puts("syslinux: warning: ");
66 puts(msg);
67 putchar('\n');
71 * read/write wrapper functions
73 int creat(const char *filename, int mode)
75 uint16_t rv;
76 uint8_t err;
78 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
80 rv = 0x3C00;
81 asm volatile ("int $0x21 ; setc %0"
82 : "=bcdm" (err), "+a" (rv)
83 : "c" (mode), "d" (filename));
84 if (err) {
85 dprintf("rv = %d\n", rv);
86 die("cannot open ldlinux.sys");
89 return rv;
92 void close(int fd)
94 uint16_t rv = 0x3E00;
96 dprintf("close(%d)\n", fd);
98 asm volatile ("int $0x21":"+a" (rv)
99 :"b"(fd));
101 /* The only error MS-DOS returns for close is EBADF,
102 and we really don't care... */
105 int rename(const char *oldname, const char *newname)
107 uint16_t rv = 0x5600; /* Also support 43FFh? */
108 uint8_t err;
110 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
112 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
113 :"d"(oldname), "D"(newname));
115 if (err) {
116 dprintf("rv = %d\n", rv);
117 warning("cannot move ldlinux.sys");
118 return rv;
121 return 0;
124 ssize_t write_file_seg(int fd, unsigned char *buf, const unsigned int len)
126 uint16_t seg = ((size_t)buf >> 4) + ds();
127 uint32_t offset = 0;
128 uint16_t rv;
129 uint8_t err;
131 while (offset < len) {
132 uint32_t chunk = 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)(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 = 0x0001;
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 if (dos_version >= 0x070a) {
192 /* Try FAT32-aware system call first */
193 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
194 "1:"
195 : "=a" (errnum)
196 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
197 "S" (1), "m" (dio)
198 : "memory");
199 dprintf(" rv(7305) = %04x", errnum);
202 /* If not supported, try the legacy system call (int2526.S) */
203 if (errnum == 0x0001)
204 errnum = int26_write_sector(drive, &dio);
206 if (errnum) {
207 dprintf("rv = %04x\n", errnum);
208 die("sector write error");
212 void read_device(int drive, void *buf, size_t nsecs, unsigned int sector)
214 uint16_t errnum = 0x0001;
215 struct diskio dio;
217 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
219 dio.startsector = sector;
220 dio.sectors = nsecs;
221 dio.bufoffs = (uintptr_t) buf;
222 dio.bufseg = data_segment();
224 if (dos_version >= 0x070a) {
225 /* Try FAT32-aware system call first */
226 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
227 "1:"
228 : "=a" (errnum)
229 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
230 "S" (0), "m" (dio));
231 dprintf(" rv(7305) = %04x", errnum);
234 /* If not supported, try the legacy system call (int2526.S) */
235 if (errnum == 0x0001)
236 errnum = int25_read_sector(drive, &dio);
238 if (errnum) {
239 dprintf("rv = %04x\n", errnum);
240 die("sector read error");
244 /* Both traditional DOS and FAT32 DOS return this structure, but
245 FAT32 return a lot more data, so make sure we have plenty of space */
246 struct deviceparams {
247 uint8_t specfunc;
248 uint8_t devtype;
249 uint16_t devattr;
250 uint16_t cylinders;
251 uint8_t mediatype;
252 uint16_t bytespersec;
253 uint8_t secperclust;
254 uint16_t ressectors;
255 uint8_t fats;
256 uint16_t rootdirents;
257 uint16_t sectors;
258 uint8_t media;
259 uint16_t fatsecs;
260 uint16_t secpertrack;
261 uint16_t heads;
262 uint32_t hiddensecs;
263 uint32_t hugesectors;
264 uint8_t lotsofpadding[224];
265 } __attribute__ ((packed));
267 uint32_t get_partition_offset(int drive)
269 uint8_t err;
270 uint16_t rv;
271 struct deviceparams dp;
273 dp.specfunc = 1; /* Get current information */
275 rv = 0x440d;
276 asm volatile ("int $0x21 ; setc %0"
277 :"=abcdm" (err), "+a"(rv), "=m"(dp)
278 :"b" (drive), "c" (0x0860), "d" (&dp));
280 if (!err)
281 return dp.hiddensecs;
283 rv = 0x440d;
284 asm volatile ("int $0x21 ; setc %0"
285 : "=abcdm" (err), "+a" (rv), "=m" (dp)
286 : "b" (drive), "c" (0x4860), "d" (&dp));
288 if (!err)
289 return dp.hiddensecs;
291 die("could not find partition start offset");
294 struct rwblock {
295 uint8_t special;
296 uint16_t head;
297 uint16_t cylinder;
298 uint16_t firstsector;
299 uint16_t sectors;
300 uint16_t bufferoffset;
301 uint16_t bufferseg;
302 } __attribute__ ((packed));
304 static struct rwblock mbr = {
305 .special = 0,
306 .head = 0,
307 .cylinder = 0,
308 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
309 .sectors = 1,
310 .bufferoffset = 0,
311 .bufferseg = 0
314 void write_mbr(int drive, const void *buf)
316 uint16_t rv;
317 uint8_t err;
319 dprintf("write_mbr(%d,%p)", drive, buf);
321 mbr.bufferoffset = (uintptr_t) buf;
322 mbr.bufferseg = data_segment();
324 rv = 0x440d;
325 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
326 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
328 dprintf(" rv(0841) = %04x", rv);
329 if (!err) {
330 dprintf("\n");
331 return;
334 rv = 0x440d;
335 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
336 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
338 dprintf(" rv(4841) = %04x\n", rv);
339 if (err)
340 die("mbr write error");
343 void read_mbr(int drive, const void *buf)
345 uint16_t rv;
346 uint8_t err;
348 dprintf("read_mbr(%d,%p)", drive, buf);
350 mbr.bufferoffset = (uintptr_t) buf;
351 mbr.bufferseg = data_segment();
353 rv = 0x440d;
354 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
355 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
357 dprintf(" rv(0861) = %04x", rv);
358 if (!err) {
359 dprintf("\n");
360 return;
363 rv = 0x440d;
364 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
365 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
367 dprintf(" rv(4841) = %04x\n", rv);
368 if (err)
369 die("mbr read error");
371 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
372 ((const uint8_t *)buf)[0],
373 ((const uint8_t *)buf)[1],
374 ((const uint8_t *)buf)[2],
375 ((const uint8_t *)buf)[3],
376 ((const uint8_t *)buf)[4],
377 ((const uint8_t *)buf)[5],
378 ((const uint8_t *)buf)[6],
379 ((const uint8_t *)buf)[7]);
382 /* This call can legitimately fail, and we don't care, so ignore error return */
383 void set_attributes(const char *file, int attributes)
385 uint16_t rv = 0x4301;
387 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
389 asm volatile ("int $0x21":"+a" (rv)
390 :"c"(attributes), "d"(file));
394 * Version of the read_device function suitable for libfat
396 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
397 libfat_sector_t sector)
399 read_device(pp, buf, 1, sector);
400 return secsize;
403 static inline void get_dos_version(void)
405 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
408 /* The locking interface relies on static variables. A massive hack :( */
409 static uint8_t lock_level, lock_drive;
411 static inline void set_lock_device(uint8_t device)
413 lock_level = 0;
414 lock_drive = device;
417 static int do_lock(uint8_t level)
419 uint16_t level_arg = lock_drive + (level << 8);
420 uint16_t rv;
421 uint8_t err;
422 #if 0
423 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
424 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
425 #else
426 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
427 #endif
429 dprintf("Trying lock %04x... ", level_arg);
430 asm volatile ("int $0x21 ; setc %0"
431 : "=bcdm" (err), "=a" (rv)
432 : "a" (0x440d), "b" (level_arg),
433 "c" (lock_call), "d" (0x0001));
434 dprintf("%s %04x\n", err ? "err" : "ok", rv);
436 return err ? rv : 0;
439 void lock_device(int level)
441 static int hard_lock = 0;
442 int err;
444 if (dos_version < 0x0700)
445 return; /* Win9x/NT only */
447 if (!hard_lock) {
448 /* Assume hierarchial "soft" locking supported */
450 while (lock_level < level) {
451 int new_level = lock_level + 1;
452 err = do_lock(new_level);
453 if (err) {
454 if (err == 0x0001) {
455 /* Try hard locking next */
456 hard_lock = 1;
458 goto soft_fail;
461 lock_level = new_level;
463 return;
466 soft_fail:
467 if (hard_lock) {
468 /* Hard locking, only level 4 supported */
469 /* This is needed for Win9x in DOS mode */
471 err = do_lock(4);
472 if (err) {
473 if (err == 0x0001) {
474 /* Assume locking is not needed */
475 return;
477 goto hard_fail;
480 lock_level = 4;
481 return;
484 hard_fail:
485 die("could not lock device");
488 void unlock_device(int level)
490 uint16_t rv;
491 uint8_t err;
492 uint16_t unlock_call;
494 if (dos_version < 0x0700)
495 return; /* Win9x/NT only */
497 #if 0
498 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
499 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
500 #else
501 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
502 #endif
504 if (lock_level == 4 && level > 0)
505 return; /* Only drop the hard lock at the end */
507 while (lock_level > level) {
508 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
509 uint16_t level_arg = (new_level << 8) + lock_drive;
510 rv = 0x440d;
511 dprintf("Trying unlock %04x... ", new_level);
512 asm volatile ("int $0x21 ; setc %0"
513 : "=bcdm" (err), "+a" (rv)
514 : "b" (level_arg), "c" (unlock_call));
515 dprintf("%s %04x\n", err ? "err" : "ok", rv);
516 lock_level = new_level;
521 * This function does any desired MBR manipulation; called with the device lock held.
523 struct mbr_entry {
524 uint8_t active; /* Active flag */
525 uint8_t bhead; /* Begin head */
526 uint8_t bsector; /* Begin sector */
527 uint8_t bcylinder; /* Begin cylinder */
528 uint8_t filesystem; /* Filesystem value */
529 uint8_t ehead; /* End head */
530 uint8_t esector; /* End sector */
531 uint8_t ecylinder; /* End cylinder */
532 uint32_t startlba; /* Start sector LBA */
533 uint32_t sectors; /* Length in sectors */
534 } __attribute__ ((packed));
536 static void adjust_mbr(int device, int writembr, int set_active)
538 static unsigned char sectbuf[SECTOR_SIZE];
539 int i;
541 if (!writembr && !set_active)
542 return; /* Nothing to do */
544 read_mbr(device, sectbuf);
546 if (writembr) {
547 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
548 *(uint16_t *) (sectbuf + 510) = 0xaa55;
551 if (set_active) {
552 uint32_t offset = get_partition_offset(device);
553 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
554 int found = 0;
556 dprintf("Searching for partition offset: %08x\n", offset);
558 for (i = 0; i < 4; i++) {
559 if (me->startlba == offset) {
560 me->active = 0x80;
561 found++;
562 } else {
563 me->active = 0;
565 me++;
568 if (found < 1) {
569 die("partition not found (-a is not implemented for logical partitions)");
570 } else if (found > 1) {
571 die("multiple aliased partitions found");
575 write_mbr(device, sectbuf);
578 static void move_file(int dev_fd, char *pathname, char *filename)
580 char new_name[160];
581 char *cp = new_name + 3;
582 const char *sd;
583 int slash = 1;
585 new_name[0] = dev_fd | 0x40;
586 new_name[1] = ':';
587 new_name[2] = '\\';
589 for (sd = opt.directory; *sd; sd++) {
590 char c = *sd;
592 if (c == '/' || c == '\\') {
593 if (slash)
594 continue;
595 c = '\\';
596 slash = 1;
597 } else {
598 slash = 0;
601 *cp++ = c;
604 /* Skip if subdirectory == root */
605 if (cp > new_name + 3) {
606 if (!slash)
607 *cp++ = '\\';
609 memcpy(cp, filename, 12);
611 set_attributes(pathname, 0);
612 if (rename(pathname, new_name))
613 set_attributes(pathname, 0x07);
614 else
615 set_attributes(new_name, 0x07);
619 int main(int argc, char *argv[])
621 static unsigned char sectbuf[SECTOR_SIZE];
622 int dev_fd, fd;
623 static char ldlinux_name[] = "@:\\ldlinux.sys";
624 static char ldlinuxc32_name[] = "@:\\ldlinux.c32";
625 struct libfat_filesystem *fs;
626 libfat_sector_t s, *secp;
627 libfat_sector_t *sectors;
628 int ldlinux_sectors;
629 int32_t ldlinux_cluster;
630 int nsectors;
631 const char *errmsg;
632 int i;
633 int patch_sectors;
634 unsigned char *dp;
636 dprintf("argv = %p\n", argv);
637 for (i = 0; i <= argc; i++)
638 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
640 get_dos_version();
642 argv[0] = program;
643 parse_options(argc, argv, MODE_SYSLINUX_DOSWIN);
645 if (!opt.device)
646 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
647 if (opt.sectors || opt.heads || opt.reset_adv || opt.set_once
648 || (opt.update_only > 0) || opt.menu_save || opt.offset) {
649 fprintf(stderr,
650 "At least one specified option not yet implemented"
651 " for this installer.\n");
652 exit(1);
656 * Create an ADV in memory... this should be smarter.
658 syslinux_reset_adv(syslinux_adv);
661 * Figure out which drive we're talking to
663 dev_fd = (opt.device[0] & ~0x20) - 0x40;
664 if (dev_fd < 1 || dev_fd > 26 || opt.device[1] != ':' || opt.device[2])
665 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
667 set_lock_device(dev_fd);
669 lock_device(2); /* Make sure we can lock the device */
670 read_device(dev_fd, sectbuf, 1, 0);
671 unlock_device(1);
674 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
676 if ((errmsg = syslinux_check_bootsect(sectbuf, NULL))) {
677 unlock_device(0);
678 puts(errmsg);
679 putchar('\n');
680 exit(1);
683 ldlinux_name[0] = dev_fd | 0x40;
684 ldlinuxc32_name[0] = dev_fd | 0x40;
686 set_attributes(ldlinux_name, 0);
687 fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */
688 write_file_seg(fd, syslinux_ldlinux, syslinux_ldlinux_len);
689 write_file(fd, syslinux_adv, 2 * ADV_SIZE);
690 close(fd);
691 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
693 set_attributes(ldlinuxc32_name, 0);
694 fd = creat(ldlinuxc32_name, 0); /* SYSTEM HIDDEN READONLY */
695 write_file_seg(fd, syslinux_ldlinuxc32, syslinux_ldlinuxc32_len);
696 close(fd);
697 set_attributes(ldlinuxc32_name, 0x07); /* SYSTEM HIDDEN READONLY */
700 * Now, use libfat to create a block map. This probably
701 * should be changed to use ioctl(...,FIBMAP,...) since
702 * this is supposed to be a simple, privileged version
703 * of the installer.
705 ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE
706 + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
707 sectors = calloc(ldlinux_sectors, sizeof *sectors);
708 lock_device(2);
709 fs = libfat_open(libfat_xpread, dev_fd);
710 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
711 secp = sectors;
712 nsectors = 0;
713 s = libfat_clustertosector(fs, ldlinux_cluster);
714 while (s && nsectors < ldlinux_sectors) {
715 *secp++ = s;
716 nsectors++;
717 s = libfat_nextsector(fs, s);
719 libfat_close(fs);
722 * If requested, move ldlinux.sys
724 if (opt.directory) {
725 move_file(dev_fd, ldlinux_name, "ldlinux.sys");
726 move_file(dev_fd, ldlinuxc32_name, "ldlinux.c32");
730 * Patch ldlinux.sys and the boot sector
732 i = syslinux_patch(sectors, nsectors, opt.stupid_mode, opt.raid_mode, opt.directory, NULL);
733 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
736 * Overwrite the now-patched ldlinux.sys
738 /* lock_device(3); -- doesn't seem to be needed */
739 dp = syslinux_ldlinux;
740 for (i = 0; i < patch_sectors; i++) {
741 memcpy_from_sl(sectbuf, dp, SECTOR_SIZE);
742 dp += SECTOR_SIZE;
743 write_device(dev_fd, sectbuf, 1, sectors[i]);
747 * Muck with the MBR, if desired, while we hold the lock
749 adjust_mbr(dev_fd, opt.install_mbr, opt.activate_partition);
752 * To finish up, write the boot sector
755 /* Read the superblock again since it might have changed while mounted */
756 read_device(dev_fd, sectbuf, 1, 0);
758 /* Copy the syslinux code into the boot sector */
759 syslinux_make_bootsect(sectbuf, VFAT);
761 /* Write new boot sector */
762 if (opt.bootsecfile) {
763 unlock_device(0);
764 fd = creat(opt.bootsecfile, 0x20); /* ARCHIVE */
765 write_file(fd, sectbuf, SECTOR_SIZE);
766 close(fd);
767 } else {
768 write_device(dev_fd, sectbuf, 1, 0);
769 unlock_device(0);
772 /* Done! */
774 return 0;