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