Merge branch 'pathbased' of ssh://terminus.zytor.com/pub/git/syslinux/syslinux into...
[profile/ivi/syslinux.git] / libinstaller / syslxmod.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2009-2010 Intel Corporation; author H. Peter Anvin
5  *
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.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * syslxmod.c - Code to provide a SYSLINUX code set to an installer.
16  */
17
18 #define _XOPEN_SOURCE 500       /* Required on glibc 2.x */
19 #define _BSD_SOURCE
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25
26 #include "syslinux.h"
27 #include "syslxint.h"
28
29 void syslinux_make_bootsect(void *bs)
30 {
31     struct boot_sector *bootsect = bs;
32     const struct boot_sector *sbs =
33         (const struct boot_sector *)boot_sector;
34
35     memcpy(&bootsect->bsHead, &sbs->bsHead, bsHeadLen);
36     memcpy(&bootsect->bsCode, &sbs->bsCode, bsCodeLen);
37 }
38
39 /*
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;
42  */
43 const char *syslinux_check_bootsect(const void *bs)
44 {
45     int veryold;
46     int sectorsize;
47     long long sectors, fatsectors, dsectors;
48     long long clusters;
49     int rootdirents, clustersize;
50     const struct boot_sector *sectbuf = bs;
51
52     veryold = 0;
53
54     /* Must be 0xF0 or 0xF8..0xFF */
55     if (get_8(&sectbuf->bsMedia) != 0xF0 && get_8(&sectbuf->bsMedia) < 0xF8)
56         return "invalid media signature (not a FAT filesystem?)";
57
58     sectorsize = get_16(&sectbuf->bsBytesPerSec);
59     if (sectorsize == SECTOR_SIZE)
60         ;                       /* ok */
61     else if (sectorsize >= 512 && sectorsize <= 4096 &&
62              (sectorsize & (sectorsize - 1)) == 0)
63         return "unsupported sectors size";
64     else
65         return "impossible sector size";
66
67     clustersize = get_8(&sectbuf->bsSecPerClust);
68     if (clustersize == 0 || (clustersize & (clustersize - 1)))
69         return "impossible cluster size";
70
71     sectors = get_16(&sectbuf->bsSectors);
72     sectors = sectors ? sectors : get_32(&sectbuf->bsHugeSectors);
73
74     dsectors = sectors - get_16(&sectbuf->bsResSectors);
75
76     fatsectors = get_16(&sectbuf->bsFATsecs);
77     fatsectors = fatsectors ? fatsectors : get_32(&sectbuf->bs32.FATSz32);
78     fatsectors *= get_8(&sectbuf->bsFATs);
79     dsectors -= fatsectors;
80
81     rootdirents = get_16(&sectbuf->bsRootDirEnts);
82     dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize;
83
84     if (dsectors < 0)
85         return "negative number of data sectors";
86
87     if (fatsectors == 0)
88         return "zero FAT sectors";
89
90     clusters = dsectors / clustersize;
91
92     if (clusters < 0xFFF5) {
93         /* FAT12 or FAT16 */
94
95         if (!get_16(&sectbuf->bsFATsecs))
96             return "zero FAT sectors (FAT12/16)";
97
98         if (get_8(&sectbuf->bs16.BootSignature) == 0x29) {
99             if (!memcmp(&sectbuf->bs16.FileSysType, "FAT12   ", 8)) {
100                 if (clusters >= 0xFF5)
101                     return "more than 4084 clusters but claims FAT12";
102             } else if (!memcmp(&sectbuf->bs16.FileSysType, "FAT16   ", 8)) {
103                 if (clusters < 0xFF5)
104                     return "less than 4084 clusters but claims FAT16";
105             } else if (!memcmp(&sectbuf->bs16.FileSysType, "FAT32   ", 8)) {
106                     return "less than 65525 clusters but claims FAT32";
107             } else if (memcmp(&sectbuf->bs16.FileSysType, "FAT     ", 8)) {
108                 static char fserr[] =
109                     "filesystem type \"????????\" not supported";
110                 memcpy(fserr + 17, &sectbuf->bs16.FileSysType, 8);
111                 return fserr;
112             }
113         }
114     } else if (clusters < 0x0FFFFFF5) {
115         /*
116          * FAT32...
117          *
118          * Moving the FileSysType and BootSignature was a lovely stroke
119          * of M$ idiocy...
120          */
121         if (get_8(&sectbuf->bs32.BootSignature) != 0x29 ||
122             memcmp(&sectbuf->bs32.FileSysType, "FAT32   ", 8))
123             return "missing FAT32 signature";
124     } else {
125         return "impossibly large number of clusters";
126     }
127
128     return NULL;
129 }
130
131 /*
132  * Special handling for the MS-DOS derivative: syslinux_ldlinux
133  * is a "far" object...
134  */
135 #ifdef __MSDOS__
136
137 #define __noinline __attribute__((noinline))
138
139 extern uint16_t ldlinux_seg;    /* Defined in dos/syslinux.c */
140
141 static inline __attribute__ ((const))
142 uint16_t ds(void)
143 {
144     uint16_t v;
145 asm("movw %%ds,%0":"=rm"(v));
146     return v;
147 }
148
149 static inline void *set_fs(const void *p)
150 {
151     uint16_t seg;
152
153     seg = ldlinux_seg + ((size_t) p >> 4);
154     asm volatile ("movw %0,%%fs"::"rm" (seg));
155     return (void *)((size_t) p & 0xf);
156 }
157
158 #if 0                           /* unused */
159 static __noinline uint8_t get_8_sl(const uint8_t * p)
160 {
161     uint8_t v;
162
163     p = set_fs(p);
164     asm volatile("movb %%fs:%1,%0":"=q" (v):"m"(*p));
165     return v;
166 }
167 #endif
168
169 static __noinline uint16_t get_16_sl(const uint16_t * p)
170 {
171     uint16_t v;
172
173     p = set_fs(p);
174     asm volatile("movw %%fs:%1,%0":"=r" (v):"m"(*p));
175     return v;
176 }
177
178 static __noinline uint32_t get_32_sl(const uint32_t * p)
179 {
180     uint32_t v;
181
182     p = set_fs(p);
183     asm volatile("movl %%fs:%1,%0":"=r" (v):"m"(*p));
184     return v;
185 }
186
187 #if 0                           /* unused */
188 static __noinline uint64_t get_64_sl(const uint64_t * p)
189 {
190     uint32_t v0, v1;
191     const uint32_t *pp = (const uint32_t *)p;
192
193     p = set_fs(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);
197 }
198 #endif
199
200 #if 0                           /* unused */
201 static __noinline void set_8_sl(uint8_t * p, uint8_t v)
202 {
203     p = set_fs(p);
204     asm volatile("movb %1,%%fs:%0":"=m" (*p):"qi"(v));
205 }
206 #endif
207
208 static __noinline void set_16_sl(uint16_t * p, uint16_t v)
209 {
210     p = set_fs(p);
211     asm volatile("movw %1,%%fs:%0":"=m" (*p):"ri"(v));
212 }
213
214 static __noinline void set_32_sl(uint32_t * p, uint32_t v)
215 {
216     p = set_fs(p);
217     asm volatile("movl %1,%%fs:%0":"=m" (*p):"ri"(v));
218 }
219
220 static __noinline void set_64_sl(uint64_t * p, uint64_t v)
221 {
222     uint32_t *pp = (uint32_t *)p;
223
224     p = set_fs(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)));
227 }
228
229 #else
230
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)
240
241 #endif
242
243 /*
244  * Generate sector extents
245  */
246 static void generate_extents(struct syslinux_extent *ex, int nptrs,
247                              const sector_t *sectp, int nsect)
248 {
249     uint32_t addr = 0x7c00 + 2*SECTOR_SIZE;
250     uint32_t base;
251     sector_t sect, lba;
252     unsigned int len;
253
254     len = lba = base = 0;
255
256     memset(ex, 0, nptrs * sizeof *ex);
257
258     while (nsect) {
259         sect = *sectp++;
260
261         if (len && sect == lba + len &&
262             ((addr ^ (base + len * SECTOR_SIZE)) & 0xffff0000) == 0) {
263             /* We can add to the current extent */
264             len++;
265             goto next;
266         }
267
268         if (len) {
269             set_64_sl(&ex->lba, lba);
270             set_16_sl(&ex->len, len);
271             ex++;
272         }
273
274         base = addr;
275         lba  = sect;
276         len  = 1;
277
278     next:
279         addr += SECTOR_SIZE;
280         nsect--;
281     }
282
283     if (len) {
284         set_64_sl(&ex->lba, lba);
285         set_16_sl(&ex->len, len);
286         ex++;
287     }
288 }
289
290 /*
291  * This patches the boot sector and the beginning of ldlinux.sys
292  * based on an ldlinux.sys sector map passed in.  Typically this is
293  * handled by writing ldlinux.sys, mapping it, and then overwrite it
294  * with the patched version.  If this isn't safe to do because of
295  * an OS which does block reallocation, then overwrite it with
296  * direct access since the location is known.
297  *
298  * Returns the number of modified bytes in ldlinux.sys if successful,
299  * otherwise -1.
300  */
301 #define NADV 2
302
303 int syslinux_patch(const sector_t *sectp, int nsectors,
304                    int stupid, int raid_mode, const char *subdir)
305 {
306     struct patch_area *patcharea;
307     struct syslinux_extent *ex;
308     uint32_t *wp;
309     int nsect = ((boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + 2;
310     uint32_t csum;
311     int i, dw, nptrs;
312     struct boot_sector *sbs = (struct boot_sector *)boot_sector;
313     size_t diroffset, dirlen;
314     int secptroffset;
315     uint64_t *advptrs;
316
317     if (nsectors <= nsect)
318         return -1;
319
320     /* Handle RAID mode, write proper bsSignature */
321     i = get_16(&sbs->bsSignature);
322     if (raid_mode)
323         set_16((uint16_t *) ((char *)sbs + i), 0x18CD); /* INT 18h */
324     set_16(&sbs->bsSignature, 0xAA55);
325
326     /* First sector need pointer in boot sector */
327     set_64(&sbs->NextSector, *sectp++);
328
329     /* Search for LDLINUX_MAGIC to find the patch area */
330     for (wp = (uint32_t *)boot_image; get_32_sl(wp) != LDLINUX_MAGIC;
331          wp++) ;
332     patcharea = (struct patch_area *)wp;
333
334     /* Set up the totals */
335     dw = boot_image_len >> 2;   /* COMPLETE dwords, excluding ADV */
336     set_16_sl(&patcharea->data_sectors, nsect - 2); /* Not including ADVs */
337     set_16_sl(&patcharea->adv_sectors, 2);      /* ADVs need 2 sectors */
338     set_32_sl(&patcharea->dwords, dw);
339
340     /* Handle Stupid mode */
341     if (stupid) {
342         /* Access only one sector at a time */
343         set_16(&patcharea->maxtransfer, 1);
344     }
345
346     /* Set the sector extents */
347     secptroffset = get_16_sl(&patcharea->secptroffset);
348     ex = (struct syslinux_extent *) ((char *)boot_image + secptroffset);
349     nptrs = get_16_sl(&patcharea->secptrcnt);
350
351     if (nsect > nptrs) {
352         /* Not necessarily an error in this case, but a general problem */
353         fprintf(stderr, "Insufficient extent space, build error!\n");
354         exit(1);
355     }
356
357     /* -1 for the pointer in the boot sector, -2 for the two ADVs */
358     generate_extents(ex, nptrs, sectp, nsect-1-2);
359
360     /* ADV pointers */
361     advptrs = (uint64_t *)((char *)boot_image +
362                            get_16_sl(&patcharea->advptroffset));
363     set_64_sl(&advptrs[0], sectp[nsect-1-2]);
364     set_64_sl(&advptrs[1], sectp[nsect-1-1]);
365
366     /* Poke in the base directory path */
367     if (subdir) {
368         diroffset = get_16(&patcharea->diroffset);
369         dirlen = get_16(&patcharea->dirlen);
370         if (dirlen <= strlen(subdir)) {
371             fprintf(stderr, "Subdirectory path too long... aborting install!\n");
372             exit(1);
373         }
374         memcpy((char *)boot_image + diroffset, subdir, strlen(subdir) + 1);
375     }
376
377     /* Now produce a checksum */
378     set_32_sl(&patcharea->checksum, 0);
379
380     csum = LDLINUX_MAGIC;
381     for (i = 0, wp = (uint32_t *)boot_image; i < dw; i++, wp++)
382         csum -= get_32_sl(wp);  /* Negative checksum */
383
384     set_32_sl(&patcharea->checksum, csum);
385
386     return dw << 2;
387 }