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
36 char *program
= "syslinux.com"; /* Name of program */
40 # define dprintf printf
45 asm volatile("int $0x16" : "=a" (ax
) : "a" (0));
48 # define dprintf(...) ((void)0)
49 # define pause() ((void)0)
52 void unlock_device(int);
54 void __attribute__ ((noreturn
)) die(const char *msg
)
63 void warning(const char *msg
)
65 puts("syslinux: warning: ");
71 * read/write wrapper functions
73 int creat(const char *filename
, int mode
)
78 dprintf("creat(\"%s\", 0x%x)\n", filename
, mode
);
81 asm volatile ("int $0x21 ; setc %0"
82 : "=bcdm" (err
), "+a" (rv
)
83 : "c" (mode
), "d" (filename
));
85 dprintf("rv = %d\n", rv
);
86 die("cannot open ldlinux.sys");
96 dprintf("close(%d)\n", fd
);
98 asm volatile ("int $0x21":"+a" (rv
)
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? */
110 dprintf("rename(\"%s\", \"%s\")\n", oldname
, newname
);
112 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err
), "+a"(rv
)
113 :"d"(oldname
), "D"(newname
));
116 dprintf("rv = %d\n", rv
);
117 warning("cannot move ldlinux.sys");
124 ssize_t
write_file_seg(int fd
, unsigned char *buf
, const unsigned int len
)
126 uint16_t seg
= ((size_t)buf
>> 4) + ds();
131 while (offset
< len
) {
132 uint32_t chunk
= 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)(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
)
181 uint16_t errnum
= 0x0001;
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 if (dos_version
>= 0x070a) {
192 /* Try FAT32-aware system call first */
193 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
196 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
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
);
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;
217 dprintf("read_device(%d,%p,%u,%u)\n", drive
, buf
, nsecs
, sector
);
219 dio
.startsector
= sector
;
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"
229 : "a" (0x7305), "b" (&dio
), "c" (-1), "d" (drive
),
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
);
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
{
252 uint16_t bytespersec
;
256 uint16_t rootdirents
;
260 uint16_t secpertrack
;
263 uint32_t hugesectors
;
264 uint8_t lotsofpadding
[224];
265 } __attribute__ ((packed
));
267 uint32_t get_partition_offset(int drive
)
271 struct deviceparams dp
;
273 dp
.specfunc
= 1; /* Get current information */
276 asm volatile ("int $0x21 ; setc %0"
277 :"=abcdm" (err
), "+a"(rv
), "=m"(dp
)
278 :"b" (drive
), "c" (0x0860), "d" (&dp
));
281 return dp
.hiddensecs
;
284 asm volatile ("int $0x21 ; setc %0"
285 : "=abcdm" (err
), "+a" (rv
), "=m" (dp
)
286 : "b" (drive
), "c" (0x4860), "d" (&dp
));
289 return dp
.hiddensecs
;
291 die("could not find partition start offset");
298 uint16_t firstsector
;
300 uint16_t bufferoffset
;
302 } __attribute__ ((packed
));
304 static struct rwblock mbr
= {
308 .firstsector
= 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
314 void write_mbr(int drive
, const void *buf
)
319 dprintf("write_mbr(%d,%p)", drive
, buf
);
321 mbr
.bufferoffset
= (uintptr_t) buf
;
322 mbr
.bufferseg
= data_segment();
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
);
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
);
340 die("mbr write error");
343 void read_mbr(int drive
, const void *buf
)
348 dprintf("read_mbr(%d,%p)", drive
, buf
);
350 mbr
.bufferoffset
= (uintptr_t) buf
;
351 mbr
.bufferseg
= data_segment();
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
);
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
);
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
);
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
)
417 static int do_lock(uint8_t level
)
419 uint16_t level_arg
= lock_drive
+ (level
<< 8);
423 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
424 uint16_t lock_call
= (dos_version
>= 0x070a) ? 0x484A : 0x084A;
426 uint16_t lock_call
= 0x084A; /* MSDN says this is OK for all filesystems */
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
);
439 void lock_device(int level
)
441 static int hard_lock
= 0;
444 if (dos_version
< 0x0700)
445 return; /* Win9x/NT only */
448 /* Assume hierarchial "soft" locking supported */
450 while (lock_level
< level
) {
451 int new_level
= lock_level
+ 1;
452 err
= do_lock(new_level
);
455 /* Try hard locking next */
461 lock_level
= new_level
;
468 /* Hard locking, only level 4 supported */
469 /* This is needed for Win9x in DOS mode */
474 /* Assume locking is not needed */
485 die("could not lock device");
488 void unlock_device(int level
)
492 uint16_t unlock_call
;
494 if (dos_version
< 0x0700)
495 return; /* Win9x/NT only */
498 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
499 unlock_call
= (dos_version
>= 0x070a) ? 0x486A : 0x086A;
501 unlock_call
= 0x086A; /* MSDN says this is OK for all filesystems */
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
;
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.
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
];
541 if (!writembr
&& !set_active
)
542 return; /* Nothing to do */
544 read_mbr(device
, sectbuf
);
547 memcpy(sectbuf
, syslinux_mbr
, syslinux_mbr_len
);
548 *(uint16_t *) (sectbuf
+ 510) = 0xaa55;
552 uint32_t offset
= get_partition_offset(device
);
553 struct mbr_entry
*me
= (struct mbr_entry
*)(sectbuf
+ 446);
556 dprintf("Searching for partition offset: %08x\n", offset
);
558 for (i
= 0; i
< 4; i
++) {
559 if (me
->startlba
== offset
) {
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
)
581 char *cp
= new_name
+ 3;
585 new_name
[0] = dev_fd
| 0x40;
589 for (sd
= opt
.directory
; *sd
; sd
++) {
592 if (c
== '/' || c
== '\\') {
604 /* Skip if subdirectory == root */
605 if (cp
> new_name
+ 3) {
609 memcpy(cp
, filename
, 12);
611 set_attributes(pathname
, 0);
612 if (rename(pathname
, new_name
))
613 set_attributes(pathname
, 0x07);
615 set_attributes(new_name
, 0x07);
619 int main(int argc
, char *argv
[])
621 static unsigned char sectbuf
[SECTOR_SIZE
];
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
;
629 int32_t ldlinux_cluster
;
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
]);
643 parse_options(argc
, argv
, MODE_SYSLINUX_DOSWIN
);
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
) {
650 "At least one specified option not yet implemented"
651 " for this installer.\n");
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);
674 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
676 if ((errmsg
= syslinux_check_bootsect(sectbuf
, NULL
))) {
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
);
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
);
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
705 ldlinux_sectors
= (syslinux_ldlinux_len
+ 2 * ADV_SIZE
706 + SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
707 sectors
= calloc(ldlinux_sectors
, sizeof *sectors
);
709 fs
= libfat_open(libfat_xpread
, dev_fd
);
710 ldlinux_cluster
= libfat_searchdir(fs
, 0, "LDLINUX SYS", NULL
);
713 s
= libfat_clustertosector(fs
, ldlinux_cluster
);
714 while (s
&& nsectors
< ldlinux_sectors
) {
717 s
= libfat_nextsector(fs
, s
);
722 * If requested, move ldlinux.sys
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
);
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
) {
764 fd
= creat(opt
.bootsecfile
, 0x20); /* ARCHIVE */
765 write_file(fd
, sectbuf
, SECTOR_SIZE
);
768 write_device(dev_fd
, sectbuf
, 1, 0);