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 * syslxmod.c - Code to provide a SYSLINUX code set to an installer.
18 #define _XOPEN_SOURCE 500 /* Required on glibc 2.x */
29 void syslinux_make_bootsect(void *bs)
31 struct boot_sector *bootsect = bs;
32 const struct boot_sector *sbs =
33 (const struct boot_sector *)boot_sector;
35 memcpy(&bootsect->bsHead, &sbs->bsHead, bsHeadLen);
36 memcpy(&bootsect->bsCode, &sbs->bsCode, bsCodeLen);
40 * Check to see that what we got was indeed an MS-DOS boot sector/superblock;
41 * Return NULL if OK and otherwise an error message;
43 const char *syslinux_check_bootsect(const void *bs)
47 long long sectors, fatsectors, dsectors;
49 int rootdirents, clustersize;
50 const struct boot_sector *sectbuf = bs;
54 /* Must be 0xF0 or 0xF8..0xFF */
55 if (get_8(§buf->bsMedia) != 0xF0 && get_8(§buf->bsMedia) < 0xF8)
56 return "invalid media signature (not a FAT filesystem?)";
58 sectorsize = get_16(§buf->bsBytesPerSec);
59 if (sectorsize == SECTOR_SIZE)
61 else if (sectorsize >= 512 && sectorsize <= 4096 &&
62 (sectorsize & (sectorsize - 1)) == 0)
63 return "unsupported sectors size";
65 return "impossible sector size";
67 clustersize = get_8(§buf->bsSecPerClust);
68 if (clustersize == 0 || (clustersize & (clustersize - 1)))
69 return "impossible cluster size";
71 sectors = get_16(§buf->bsSectors);
72 sectors = sectors ? sectors : get_32(§buf->bsHugeSectors);
74 dsectors = sectors - get_16(§buf->bsResSectors);
76 fatsectors = get_16(§buf->bsFATsecs);
77 fatsectors = fatsectors ? fatsectors : get_32(§buf->bs32.FATSz32);
78 fatsectors *= get_8(§buf->bsFATs);
79 dsectors -= fatsectors;
81 rootdirents = get_16(§buf->bsRootDirEnts);
82 dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize;
85 return "negative number of data sectors";
88 return "zero FAT sectors";
90 clusters = dsectors / clustersize;
92 if (clusters < 0xFFF5) {
95 if (!get_16(§buf->bsFATsecs))
96 return "zero FAT sectors (FAT12/16)";
98 if (get_8(§buf->bs16.BootSignature) == 0x29) {
99 if (!memcmp(§buf->bs16.FileSysType, "FAT12 ", 8)) {
100 if (clusters >= 0xFF5)
101 return "more than 4084 clusters but claims FAT12";
102 } else if (!memcmp(§buf->bs16.FileSysType, "FAT16 ", 8)) {
103 if (clusters < 0xFF5)
104 return "less than 4084 clusters but claims FAT16";
105 } else if (!memcmp(§buf->bs16.FileSysType, "FAT32 ", 8)) {
106 return "less than 65525 clusters but claims FAT32";
107 } else if (memcmp(§buf->bs16.FileSysType, "FAT ", 8)) {
108 static char fserr[] =
109 "filesystem type \"????????\" not supported";
110 memcpy(fserr + 17, §buf->bs16.FileSysType, 8);
114 } else if (clusters < 0x0FFFFFF5) {
118 * Moving the FileSysType and BootSignature was a lovely stroke
121 if (get_8(§buf->bs32.BootSignature) != 0x29 ||
122 memcmp(§buf->bs32.FileSysType, "FAT32 ", 8))
123 return "missing FAT32 signature";
125 return "impossibly large number of clusters";
132 * Special handling for the MS-DOS derivative: syslinux_ldlinux
133 * is a "far" object...
137 #define __noinline __attribute__((noinline))
139 extern uint16_t ldlinux_seg; /* Defined in dos/syslinux.c */
141 static inline __attribute__ ((const))
145 asm("movw %%ds,%0":"=rm"(v));
149 static inline void *set_fs(const void *p)
153 seg = ldlinux_seg + ((size_t) p >> 4);
154 asm volatile ("movw %0,%%fs"::"rm" (seg));
155 return (void *)((size_t) p & 0xf);
159 static __noinline uint8_t get_8_sl(const uint8_t * p)
164 asm volatile("movb %%fs:%1,%0":"=q" (v):"m"(*p));
169 static __noinline uint16_t get_16_sl(const uint16_t * p)
174 asm volatile("movw %%fs:%1,%0":"=r" (v):"m"(*p));
178 static __noinline uint32_t get_32_sl(const uint32_t * p)
183 asm volatile("movl %%fs:%1,%0":"=r" (v):"m"(*p));
188 static __noinline uint64_t get_64_sl(const uint64_t * p)
191 const uint32_t *pp = (const uint32_t *)p;
194 asm volatile("movl %%fs:%1,%0" : "=r" (v0) : "m" (pp[0]));
195 asm volatile("movl %%fs:%1,%0" : "=r" (v1) : "m" (pp[1]));
196 return v0 + ((uint64_t)v1 << 32);
201 static __noinline void set_8_sl(uint8_t * p, uint8_t v)
204 asm volatile("movb %1,%%fs:%0":"=m" (*p):"qi"(v));
208 static __noinline void set_16_sl(uint16_t * p, uint16_t v)
211 asm volatile("movw %1,%%fs:%0":"=m" (*p):"ri"(v));
214 static __noinline void set_32_sl(uint32_t * p, uint32_t v)
217 asm volatile("movl %1,%%fs:%0":"=m" (*p):"ri"(v));
220 static __noinline void set_64_sl(uint64_t * p, uint64_t v)
222 uint32_t *pp = (uint32_t *)p;
225 asm volatile("movl %1,%%fs:%0" : "=m" (pp[0]) : "ri"((uint32_t)v));
226 asm volatile("movl %1,%%fs:%0" : "=m" (pp[1]) : "ri"((uint32_t)(v >> 32)));
231 /* Sane system ... */
232 #define get_8_sl(x) get_8(x)
233 #define get_16_sl(x) get_16(x)
234 #define get_32_sl(x) get_32(x)
235 #define get_64_sl(x) get_64(x)
236 #define set_8_sl(x,y) set_8(x,y)
237 #define set_16_sl(x,y) set_16(x,y)
238 #define set_32_sl(x,y) set_32(x,y)
239 #define set_64_sl(x,y) set_64(x,y)
244 * Generate sector extents
246 static void generate_extents(struct syslinux_extent *ex, int nptrs,
247 const sector_t *sectp, int nsect)
249 struct syslinux_extent thisex;
250 uint32_t addr = 0x7c00 + 2*SECTOR_SIZE;
254 thisex.len = base = 0;
256 memset(ex, 0, nptrs * sizeof *ex);
262 sect == thisex.lba + thisex.len &&
263 ((addr ^ (base + thisex.len * SECTOR_SIZE)) & 0xffff0000) == 0) {
264 /* We can add to the current extent */
270 set_64_sl(&ex->lba, thisex.lba);
271 set_16_sl(&ex->len, thisex.len);
285 set_64_sl(&ex->lba, thisex.lba);
286 set_16_sl(&ex->len, thisex.len);
292 * This patches the boot sector and the beginning of ldlinux.sys
293 * based on an ldlinux.sys sector map passed in. Typically this is
294 * handled by writing ldlinux.sys, mapping it, and then overwrite it
295 * with the patched version. If this isn't safe to do because of
296 * an OS which does block reallocation, then overwrite it with
297 * direct access since the location is known.
299 * Returns the number of modified bytes in ldlinux.sys if successful,
304 int syslinux_patch(const sector_t *sectp, int nsectors,
305 int stupid, int raid_mode, const char *subdir)
307 struct patch_area *patcharea;
308 struct syslinux_extent *ex;
310 int nsect = ((boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + 2;
313 struct boot_sector *sbs = (struct boot_sector *)boot_sector;
314 size_t diroffset, dirlen;
318 if (nsectors <= nsect)
321 /* Handle RAID mode, write proper bsSignature */
322 i = get_16(&sbs->bsSignature);
324 set_16((uint16_t *) ((char *)sbs + i), 0x18CD); /* INT 18h */
325 set_16(&sbs->bsSignature, 0xAA55);
327 /* First sector need pointer in boot sector */
328 set_64(&sbs->NextSector, *sectp++);
330 /* Search for LDLINUX_MAGIC to find the patch area */
331 for (wp = (uint32_t *)boot_image; get_32_sl(wp) != LDLINUX_MAGIC;
333 patcharea = (struct patch_area *)wp;
335 /* Set up the totals */
336 dw = boot_image_len >> 2; /* COMPLETE dwords, excluding ADV */
337 set_16_sl(&patcharea->data_sectors, nsect - 2); /* Not including ADVs */
338 set_16_sl(&patcharea->adv_sectors, 2); /* ADVs need 2 sectors */
339 set_32_sl(&patcharea->dwords, dw);
341 /* Handle Stupid mode */
343 /* Access only one sector at a time */
344 set_16(&patcharea->maxtransfer, 1);
347 /* Set the sector extents */
348 secptroffset = get_16_sl(&patcharea->secptroffset);
349 ex = (struct syslinux_extent *) ((char *)boot_image + secptroffset);
350 nptrs = get_16_sl(&patcharea->secptrcnt);
353 /* Not necessarily an error in this case, but a general problem */
354 fprintf(stderr, "Insufficient extent space, build error!\n");
358 /* -1 for the pointer in the boot sector, -2 for the two ADVs */
359 generate_extents(ex, nptrs, sectp, nsect-1-2);
362 advptrs = (uint64_t *)((char *)boot_image +
363 get_16_sl(&patcharea->advptroffset));
364 set_64_sl(&advptrs[0], sectp[nsect-1-2]);
365 set_64_sl(&advptrs[1], sectp[nsect-1-1]);
367 /* Poke in the base directory path */
369 diroffset = get_16(&patcharea->diroffset);
370 dirlen = get_16(&patcharea->dirlen);
371 if (dirlen <= strlen(subdir)) {
372 fprintf(stderr, "Subdirectory path too long... aborting install!\n");
375 memcpy((char *)boot_image + diroffset, subdir, strlen(subdir) + 1);
378 /* Now produce a checksum */
379 set_32_sl(&patcharea->checksum, 0);
381 csum = LDLINUX_MAGIC;
382 for (i = 0, wp = (uint32_t *)boot_image; i < dw; i++, wp++)
383 csum -= get_32_sl(wp); /* Negative checksum */
385 set_32_sl(&patcharea->checksum, csum);