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
34 const char *program
= "syslinux"; /* Name of program */
38 # define dprintf printf
43 asm volatile("int $0x16" : "=a" (ax
) : "a" (0));
46 # define dprintf(...) ((void)0)
47 # define pause() ((void)0)
50 void unlock_device(int);
52 void __attribute__ ((noreturn
)) die(const char *msg
)
61 void warning(const char *msg
)
63 puts("syslinux: warning: ");
69 * read/write wrapper functions
71 int creat(const char *filename
, int mode
)
76 dprintf("creat(\"%s\", 0x%x)\n", filename
, mode
);
79 asm volatile ("int $0x21 ; setc %0"
80 : "=bcdm" (err
), "+a" (rv
)
81 : "c" (mode
), "d" (filename
));
83 dprintf("rv = %d\n", rv
);
84 die("cannot open ldlinux.sys");
94 dprintf("close(%d)\n", fd
);
96 asm volatile ("int $0x21":"+a" (rv
)
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? */
108 dprintf("rename(\"%s\", \"%s\")\n", oldname
, newname
);
110 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err
), "+a"(rv
)
111 :"d"(oldname
), "D"(newname
));
114 dprintf("rv = %d\n", rv
);
115 warning("cannot move ldlinux.sys");
122 extern const char __payload_sseg
[];
123 uint16_t ldlinux_seg
;
125 ssize_t
write_ldlinux(int fd
)
131 while (offset
< syslinux_ldlinux_len
) {
132 uint32_t chunk
= syslinux_ldlinux_len
- offset
;
135 asm volatile ("pushw %%ds ; "
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))));
142 die("file write error");
149 ssize_t
write_file(int fd
, const void *buf
, size_t count
)
155 dprintf("write_file(%d,%p,%u)\n", fd
, buf
, count
);
158 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err
), "=a"(rv
)
159 :"a"(0x4000), "b"(fd
), "c"(count
), "d"(buf
));
161 die("file write error");
170 static inline __attribute__ ((const))
171 uint16_t data_segment(void)
175 asm("movw %%ds,%0" : "=rm"(ds
));
179 void write_device(int drive
, const void *buf
, size_t nsecs
, unsigned int sector
)
184 dprintf("write_device(%d,%p,%u,%u)\n", drive
, buf
, nsecs
, sector
);
186 dio
.startsector
= sector
;
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"
195 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
199 /* If not supported, try the legacy system call (int2526.S) */
200 if (errnum
== 0x0001)
201 errnum
= int26_write_sector(drive
, &dio
);
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
)
214 dprintf("read_device(%d,%p,%u,%u)\n", drive
, buf
, nsecs
, sector
);
216 dio
.startsector
= sector
;
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"
225 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
228 /* If not supported, try the legacy system call (int2526.S) */
229 if (errnum
== 0x0001)
230 errnum
= int25_read_sector(drive
, &dio
);
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
{
246 uint16_t bytespersec
;
250 uint16_t rootdirents
;
254 uint16_t secpertrack
;
257 uint32_t hugesectors
;
258 uint8_t lotsofpadding
[224];
259 } __attribute__ ((packed
));
261 uint32_t get_partition_offset(int drive
)
265 struct deviceparams dp
;
267 dp
.specfunc
= 1; /* Get current information */
270 asm volatile ("int $0x21 ; setc %0"
271 :"=abcdm" (err
), "+a"(rv
), "=m"(dp
)
272 :"b" (drive
), "c" (0x0860), "d" (&dp
));
275 return dp
.hiddensecs
;
278 asm volatile ("int $0x21 ; setc %0"
279 : "=abcdm" (err
), "+a" (rv
), "=m" (dp
)
280 : "b" (drive
), "c" (0x4860), "d" (&dp
));
283 return dp
.hiddensecs
;
285 die("could not find partition start offset");
292 uint16_t firstsector
;
294 uint16_t bufferoffset
;
296 } __attribute__ ((packed
));
298 static struct rwblock mbr
= {
302 .firstsector
= 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
308 void write_mbr(int drive
, const void *buf
)
313 dprintf("write_mbr(%d,%p)", drive
, buf
);
315 mbr
.bufferoffset
= (uintptr_t) buf
;
316 mbr
.bufferseg
= data_segment();
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
);
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
);
334 die("mbr write error");
337 void read_mbr(int drive
, const void *buf
)
342 dprintf("read_mbr(%d,%p)", drive
, buf
);
344 mbr
.bufferoffset
= (uintptr_t) buf
;
345 mbr
.bufferseg
= data_segment();
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
);
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
);
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
);
397 static inline void get_dos_version(void)
401 asm("int $0x21 ; xchgb %%ah,%%al"
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
)
419 static int do_lock(uint8_t level
)
421 uint16_t level_arg
= lock_drive
+ (level
<< 8);
425 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
426 uint16_t lock_call
= (dos_version
>= 0x070a) ? 0x484A : 0x084A;
428 uint16_t lock_call
= 0x084A; /* MSDN says this is OK for all filesystems */
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
);
441 void lock_device(int level
)
443 static int hard_lock
= 0;
446 if (dos_version
< 0x0700)
447 return; /* Win9x/NT only */
450 /* Assume hierarchial "soft" locking supported */
452 while (lock_level
< level
) {
453 int new_level
= lock_level
+ 1;
454 err
= do_lock(new_level
);
457 /* Try hard locking next */
463 lock_level
= new_level
;
470 /* Hard locking, only level 4 supported */
471 /* This is needed for Win9x in DOS mode */
476 /* Assume locking is not needed */
487 die("could not lock device");
490 void unlock_device(int level
)
494 uint16_t unlock_call
;
496 if (dos_version
< 0x0700)
497 return; /* Win9x/NT only */
500 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
501 unlock_call
= (dos_version
>= 0x070a) ? 0x486A : 0x086A;
503 unlock_call
= 0x086A; /* MSDN says this is OK for all filesystems */
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
;
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.
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
];
543 if (!writembr
&& !set_active
)
544 return; /* Nothing to do */
546 read_mbr(device
, sectbuf
);
549 memcpy(sectbuf
, syslinux_mbr
, syslinux_mbr_len
);
550 *(uint16_t *) (sectbuf
+ 510) = 0xaa55;
554 uint32_t offset
= get_partition_offset(device
);
555 struct mbr_entry
*me
= (struct mbr_entry
*)(sectbuf
+ 446);
558 dprintf("Searching for partition offset: %08x\n", offset
);
560 for (i
= 0; i
< 4; i
++) {
561 if (me
->startlba
== offset
) {
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
];
584 static char ldlinux_name
[] = "@:\\ldlinux.sys";
586 int force
= 0; /* -f (force) option */
587 struct libfat_filesystem
*fs
;
588 libfat_sector_t s
, *secp
;
589 libfat_sector_t
*sectors
;
591 int32_t ldlinux_cluster
;
593 const char *device
= NULL
, *bootsecfile
= NULL
;
596 int writembr
= 0; /* -m (write MBR) option */
597 int set_active
= 0; /* -a (set partition active) option */
598 const char *subdir
= NULL
;
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 */
613 for (argp
= argv
+ 1; *argp
; argp
++) {
617 usage(EX_USAGE
, MODE_SYSLINUX_DOSWIN
);
621 case 's': /* Use "safe, slow and stupid" code */
624 case 'r': /* RAID mode */
627 case 'f': /* Force install */
630 case 'm': /* Write MBR */
633 case 'a': /* Set partition active */
641 usage(EX_USAGE
, MODE_SYSLINUX_DOSWIN
);
647 usage(EX_USAGE
, MODE_SYSLINUX_DOSWIN
);
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);
677 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
679 if ((errmsg
= syslinux_check_bootsect(sectbuf
))) {
686 ldlinux_name
[0] = dev_fd
| 0x40;
688 set_attributes(ldlinux_name
, 0);
689 fd
= creat(ldlinux_name
, 0); /* SYSTEM HIDDEN READONLY */
691 write_file(fd
, syslinux_adv
, 2 * ADV_SIZE
);
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
701 ldlinux_sectors
= (syslinux_ldlinux_len
+ 2 * ADV_SIZE
702 + SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
703 sectors
= calloc(ldlinux_sectors
, sizeof *sectors
);
705 fs
= libfat_open(libfat_xpread
, dev_fd
);
706 ldlinux_cluster
= libfat_searchdir(fs
, 0, "LDLINUX SYS", NULL
);
709 s
= libfat_clustertosector(fs
, ldlinux_cluster
);
710 while (s
&& nsectors
< ldlinux_sectors
) {
713 s
= libfat_nextsector(fs
, s
);
718 * If requested, move ldlinux.sys
721 char new_ldlinux_name
[160];
722 char *cp
= new_ldlinux_name
+ 3;
726 new_ldlinux_name
[0] = dev_fd
| 0x40;
727 new_ldlinux_name
[1] = ':';
728 new_ldlinux_name
[2] = '\\';
730 for (sd
= subdir
; *sd
; sd
++) {
733 if (c
== '/' || c
== '\\') {
745 /* Skip if subdirectory == root */
746 if (cp
> new_ldlinux_name
+ 3) {
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);
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
++) {
773 di
= (size_t) sectbuf
;
774 cx
= SECTOR_SIZE
>> 2;
775 asm volatile ("movw %3,%%fs ; fs ; rep ; movsl":"+S" (si
), "+D"(di
),
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 */
800 fd
= creat(bootsecfile
, 0x20); /* ARCHIVE */
801 write_file(fd
, sectbuf
, SECTOR_SIZE
);
804 write_device(dev_fd
, sectbuf
, 1, 0);