1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * syslxmod.c - Code to provide a SYSLINUX code set to an installer.
17 #define _XOPEN_SOURCE 500 /* Required on glibc 2.x */
26 #define LDLINUX_MAGIC 0x3eb202fe
45 bs16DriveNumber
= 0x24,
47 bs16BootSignature
= 0x26,
49 bs16VolumeLabel
= 0x2b,
50 bs16FileSysType
= 0x36,
63 bs32BootSignature
= 66,
73 #define bsHeadLen (bsOemName-bsHead)
74 #define bsCode bs32Code /* The common safe choice */
75 #define bsCodeLen (bsSignature-bs32Code)
78 * Access functions for littleendian numbers, possibly misaligned.
80 static inline uint8_t get_8(const unsigned char *p
)
82 return *(const uint8_t *)p
;
85 static inline uint16_t get_16(const unsigned char *p
)
87 #if defined(__i386__) || defined(__x86_64__)
88 /* Littleendian and unaligned-capable */
89 return *(const uint16_t *)p
;
91 return (uint16_t)p
[0] + ((uint16_t)p
[1] << 8);
95 static inline uint32_t get_32(const unsigned char *p
)
97 #if defined(__i386__) || defined(__x86_64__)
98 /* Littleendian and unaligned-capable */
99 return *(const uint32_t *)p
;
101 return (uint32_t)p
[0] + ((uint32_t)p
[1] << 8) +
102 ((uint32_t)p
[2] << 16) + ((uint32_t)p
[3] << 24);
106 static inline void set_16(unsigned char *p
, uint16_t v
)
108 #if defined(__i386__) || defined(__x86_64__)
109 /* Littleendian and unaligned-capable */
113 p
[1] = ((v
>> 8) & 0xff);
117 static inline void set_32(unsigned char *p
, uint32_t v
)
119 #if defined(__i386__) || defined(__x86_64__)
120 /* Littleendian and unaligned-capable */
124 p
[1] = ((v
>> 8) & 0xff);
125 p
[2] = ((v
>> 16) & 0xff);
126 p
[3] = ((v
>> 24) & 0xff);
130 /* Patch the code so that we're running in stupid mode */
131 void syslinux_make_stupid(void)
133 /* Access only one sector at a time */
134 set_16(syslinux_bootsect
+0x1FC, 1);
137 void syslinux_make_bootsect(void *bs
)
139 unsigned char *bootsect
= bs
;
141 memcpy(bootsect
+bsHead
, syslinux_bootsect
+bsHead
, bsHeadLen
);
142 memcpy(bootsect
+bsCode
, syslinux_bootsect
+bsCode
, bsCodeLen
);
146 * Check to see that what we got was indeed an MS-DOS boot sector/superblock;
147 * Return NULL if OK and otherwise an error message;
149 const char *syslinux_check_bootsect(const void *bs
)
153 long long sectors
, fatsectors
, dsectors
;
155 int rootdirents
, clustersize
;
156 const unsigned char *sectbuf
= bs
;
160 /* Must be 0xF0 or 0xF8..0xFF */
161 if ( get_8(sectbuf
+bsMedia
) != 0xF0 &&
162 get_8(sectbuf
+bsMedia
) < 0xF8 )
165 sectorsize
= get_16(sectbuf
+bsBytesPerSec
);
166 if ( sectorsize
== 512 )
168 else if ( sectorsize
== 1024 || sectorsize
== 2048 || sectorsize
== 4096 )
169 return "only 512-byte sectors are supported";
173 clustersize
= get_8(sectbuf
+bsSecPerClust
);
174 if ( clustersize
== 0 || (clustersize
& (clustersize
-1)) )
175 goto invalid
; /* Must be nonzero and a power of 2 */
177 sectors
= get_16(sectbuf
+bsSectors
);
178 sectors
= sectors
? sectors
: get_32(sectbuf
+bsHugeSectors
);
180 dsectors
= sectors
- get_16(sectbuf
+bsResSectors
);
182 fatsectors
= get_16(sectbuf
+bsFATsecs
);
183 fatsectors
= fatsectors
? fatsectors
: get_32(sectbuf
+bs32FATSz32
);
184 fatsectors
*= get_8(sectbuf
+bsFATs
);
185 dsectors
-= fatsectors
;
187 rootdirents
= get_16(sectbuf
+bsRootDirEnts
);
188 dsectors
-= (rootdirents
+sectorsize
/32-1)/sectorsize
;
190 if ( dsectors
< 0 || fatsectors
== 0 )
193 clusters
= dsectors
/clustersize
;
195 if ( clusters
< 0xFFF5 ) {
198 if ( !get_16(sectbuf
+bsFATsecs
) )
201 if ( get_8(sectbuf
+bs16BootSignature
) == 0x29 ) {
202 if ( !memcmp(sectbuf
+bs16FileSysType
, "FAT12 ", 8) ) {
203 if ( clusters
>= 0xFF5 )
204 return "more than 4084 clusters but claims FAT12";
205 } else if ( !memcmp(sectbuf
+bs16FileSysType
, "FAT16 ", 8) ) {
206 if ( clusters
< 0xFF5 )
207 return "less than 4084 clusters but claims FAT16";
208 } else if ( memcmp(sectbuf
+bs16FileSysType
, "FAT ", 8) ) {
209 static char fserr
[] = "filesystem type \"????????\" not supported";
210 memcpy(fserr
+17, sectbuf
+bs16FileSysType
, 8);
214 } else if ( clusters
< 0x0FFFFFF5 ) {
216 /* Moving the FileSysType and BootSignature was a lovely stroke of M$ idiocy */
217 if ( get_8(sectbuf
+bs32BootSignature
) != 0x29 ||
218 memcmp(sectbuf
+bs32FileSysType
, "FAT32 ", 8) )
227 return "this doesn't look like a valid FAT filesystem";
231 * This patches the boot sector and the first sector of ldlinux.sys
232 * based on an ldlinux.sys sector map passed in. Typically this is
233 * handled by writing ldlinux.sys, mapping it, and then overwrite it
234 * with the patched version. If this isn't safe to do because of
235 * an OS which does block reallocation, then overwrite it with
236 * direct access since the location is known.
238 * Return 0 if successful, otherwise -1.
240 int syslinux_patch(const uint32_t *sectors
, int nsectors
)
242 unsigned char *patcharea
, *p
;
243 int nsect
= (syslinux_ldlinux_len
+511) >> 9;
247 if ( nsectors
< nsect
)
250 /* First sector need pointer in boot sector */
251 set_32(syslinux_bootsect
+0x1F8, *sectors
++);
254 /* Search for LDLINUX_MAGIC to find the patch area */
255 for ( p
= syslinux_ldlinux
; get_32(p
) != LDLINUX_MAGIC
; p
+= 4 );
258 /* Set up the totals */
259 dw
= syslinux_ldlinux_len
>> 2; /* COMPLETE dwords! */
260 set_16(patcharea
, dw
);
261 set_16(patcharea
+2, nsect
); /* Does not include the first sector! */
263 /* Set the sector pointers */
268 set_32(p
, *sectors
++);
272 /* Now produce a checksum */
273 set_32(patcharea
+4, 0);
275 csum
= LDLINUX_MAGIC
;
276 for ( i
= 0, p
= syslinux_ldlinux
; i
< dw
; i
++, p
+= 4 )
277 csum
-= get_32(p
); /* Negative checksum */
279 set_32(patcharea
+4, csum
);