summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/syslinux/syslinux/0008-libinstaller-syslinuxext-implement-syslinux_patch_bo.patch
blob: b026eba5ad4cb44b1d0396c3953d65311c5825f2 (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
From acfc8214d3d60b7e251ae66a59b81cdd1ff7a6dc Mon Sep 17 00:00:00 2001
From: Robert Yang <liezhi.yang@windriver.com>
Date: Fri, 2 Jan 2015 12:26:46 +0800
Subject: [PATCH] libinstaller/syslinuxext: implement syslinux_patch_bootsect()

Move the related from extlinux/main.c to libinstaller/syslinuxext.c, the
syslinux_patch_bootsect() are used by both extlinux/main.c and
linux/syslinux.c.

Upstream-Status: Submitted

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Tested-by: Du Dolpher <dolpher.du@intel.com>

Edited to include sysmacros.h

Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
---
 extlinux/Makefile          |   3 +-
 extlinux/main.c            | 167 +-----------------------------------
 libinstaller/syslinuxext.c | 171 +++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+), 165 deletions(-)

diff --git a/extlinux/Makefile b/extlinux/Makefile
index 1721ee5..62a4972 100644
--- a/extlinux/Makefile
+++ b/extlinux/Makefile
@@ -32,7 +32,8 @@ SRCS     = main.c \
 	   ../libinstaller/advio.c \
 	   ../libinstaller/bootsect_bin.c \
 	   ../libinstaller/ldlinuxc32_bin.c \
-	   ../libinstaller/ldlinux_bin.c
+	   ../libinstaller/ldlinux_bin.c \
+	   ../libinstaller/syslinuxext.c
 OBJS	 = $(patsubst %.c,%.o,$(notdir $(SRCS)))
 
 .SUFFIXES: .c .o .i .s .S
diff --git a/extlinux/main.c b/extlinux/main.c
index ebff7ea..9add50f 100644
--- a/extlinux/main.c
+++ b/extlinux/main.c
@@ -62,6 +62,7 @@
 #include "setadv.h"
 #include "syslxopt.h" /* unified options */
 #include "mountinfo.h"
+#include "syslinuxext.h"
 
 #ifdef DEBUG
 # define dprintf printf
@@ -69,10 +70,6 @@
 # define dprintf(...) ((void)0)
 #endif
 
-#ifndef EXT2_SUPER_OFFSET
-#define EXT2_SUPER_OFFSET 1024
-#endif
-
 /* Since we have unused 2048 bytes in the primary AG of an XFS partition,
  * we will use the first 0~512 bytes starting from 2048 for the Syslinux
  * boot sector.
@@ -93,136 +90,6 @@ static char subvol[BTRFS_SUBVOL_MAX];
 #define BTRFS_ADV_OFFSET (BTRFS_BOOT_AREA_A_OFFSET + BTRFS_BOOT_AREA_A_SIZE \
 			  - 2*ADV_SIZE)
 
-/*
- * Get the size of a block device
- */
-static uint64_t get_size(int devfd)
-{
-    uint64_t bytes;
-    uint32_t sects;
-    struct stat st;
-
-#ifdef BLKGETSIZE64
-    if (!ioctl(devfd, BLKGETSIZE64, &bytes))
-	return bytes;
-#endif
-    if (!ioctl(devfd, BLKGETSIZE, &sects))
-	return (uint64_t) sects << 9;
-    else if (!fstat(devfd, &st) && st.st_size)
-	return st.st_size;
-    else
-	return 0;
-}
-
-/*
- * Get device geometry and partition offset
- */
-struct geometry_table {
-    uint64_t bytes;
-    struct hd_geometry g;
-};
-
-static int sysfs_get_offset(int devfd, unsigned long *start)
-{
-    struct stat st;
-    char sysfs_name[128];
-    FILE *f;
-    int rv;
-
-    if (fstat(devfd, &st))
-	return -1;
-
-    if ((size_t)snprintf(sysfs_name, sizeof sysfs_name,
-			 "/sys/dev/block/%u:%u/start",
-			 major(st.st_rdev), minor(st.st_rdev))
-	>= sizeof sysfs_name)
-	return -1;
-
-    f = fopen(sysfs_name, "r");
-    if (!f)
-	return -1;
-
-    rv = fscanf(f, "%lu", start);
-    fclose(f);
-
-    return (rv == 1) ? 0 : -1;
-}
-
-/* Standard floppy disk geometries, plus LS-120.  Zipdisk geometry
-   (x/64/32) is the final fallback.  I don't know what LS-240 has
-   as its geometry, since I don't have one and don't know anyone that does,
-   and Google wasn't helpful... */
-static const struct geometry_table standard_geometries[] = {
-    {360 * 1024, {2, 9, 40, 0}},
-    {720 * 1024, {2, 9, 80, 0}},
-    {1200 * 1024, {2, 15, 80, 0}},
-    {1440 * 1024, {2, 18, 80, 0}},
-    {1680 * 1024, {2, 21, 80, 0}},
-    {1722 * 1024, {2, 21, 80, 0}},
-    {2880 * 1024, {2, 36, 80, 0}},
-    {3840 * 1024, {2, 48, 80, 0}},
-    {123264 * 1024, {8, 32, 963, 0}},	/* LS120 */
-    {0, {0, 0, 0, 0}}
-};
-
-int get_geometry(int devfd, uint64_t totalbytes, struct hd_geometry *geo)
-{
-    struct floppy_struct fd_str;
-    struct loop_info li;
-    struct loop_info64 li64;
-    const struct geometry_table *gp;
-    int rv = 0;
-
-    memset(geo, 0, sizeof *geo);
-
-    if (!ioctl(devfd, HDIO_GETGEO, geo)) {
-	goto ok;
-    } else if (!ioctl(devfd, FDGETPRM, &fd_str)) {
-	geo->heads = fd_str.head;
-	geo->sectors = fd_str.sect;
-	geo->cylinders = fd_str.track;
-	geo->start = 0;
-	goto ok;
-    }
-
-    /* Didn't work.  Let's see if this is one of the standard geometries */
-    for (gp = standard_geometries; gp->bytes; gp++) {
-	if (gp->bytes == totalbytes) {
-	    memcpy(geo, &gp->g, sizeof *geo);
-	    goto ok;
-	}
-    }
-
-    /* Didn't work either... assign a geometry of 64 heads, 32 sectors; this is
-       what zipdisks use, so this would help if someone has a USB key that
-       they're booting in USB-ZIP mode. */
-
-    geo->heads = opt.heads ? : 64;
-    geo->sectors = opt.sectors ? : 32;
-    geo->cylinders = totalbytes / (geo->heads * geo->sectors << SECTOR_SHIFT);
-    geo->start = 0;
-
-    if (!opt.sectors && !opt.heads) {
-	fprintf(stderr,
-		"Warning: unable to obtain device geometry (defaulting to %d heads, %d sectors)\n"
-		"         (on hard disks, this is usually harmless.)\n",
-		geo->heads, geo->sectors);
-	rv = 1;			/* Suboptimal result */
-    }
-
-ok:
-    /* If this is a loopback device, try to set the start */
-    if (!ioctl(devfd, LOOP_GET_STATUS64, &li64))
-	geo->start = li64.lo_offset >> SECTOR_SHIFT;
-    else if (!ioctl(devfd, LOOP_GET_STATUS, &li))
-	geo->start = (unsigned int)li.lo_offset >> SECTOR_SHIFT;
-    else if (!sysfs_get_offset(devfd, &geo->start)) {
-	/* OK */
-    }
-
-    return rv;
-}
-
 /*
  * Query the device geometry and put it into the boot sector.
  * Map the file and put the map in the boot sector and file.
@@ -233,11 +100,8 @@ ok:
 static int patch_file_and_bootblock(int fd, const char *dir, int devfd)
 {
     struct stat dirst, xdst;
-    struct hd_geometry geo;
     sector_t *sectp;
-    uint64_t totalbytes, totalsectors;
     int nsect;
-    struct fat_boot_sector *sbs;
     char *dirpath, *subpath, *xdirpath;
     int rv;
 
@@ -281,33 +145,8 @@ static int patch_file_and_bootblock(int fd, const char *dir, int devfd)
     /* Now subpath should contain the path relative to the fs base */
     dprintf("subpath = %s\n", subpath);
 
-    totalbytes = get_size(devfd);
-    get_geometry(devfd, totalbytes, &geo);
-
-    if (opt.heads)
-	geo.heads = opt.heads;
-    if (opt.sectors)
-	geo.sectors = opt.sectors;
-
-    /* Patch this into a fake FAT superblock.  This isn't because
-       FAT is a good format in any way, it's because it lets the
-       early bootstrap share code with the FAT version. */
-    dprintf("heads = %u, sect = %u\n", geo.heads, geo.sectors);
-
-    sbs = (struct fat_boot_sector *)syslinux_bootsect;
-
-    totalsectors = totalbytes >> SECTOR_SHIFT;
-    if (totalsectors >= 65536) {
-	set_16(&sbs->bsSectors, 0);
-    } else {
-	set_16(&sbs->bsSectors, totalsectors);
-    }
-    set_32(&sbs->bsHugeSectors, totalsectors);
-
-    set_16(&sbs->bsBytesPerSec, SECTOR_SIZE);
-    set_16(&sbs->bsSecPerTrack, geo.sectors);
-    set_16(&sbs->bsHeads, geo.heads);
-    set_32(&sbs->bsHiddenSecs, geo.start);
+    /* Patch syslinux_bootsect */
+    syslinux_patch_bootsect(devfd);
 
     /* Construct the boot file map */
 
diff --git a/libinstaller/syslinuxext.c b/libinstaller/syslinuxext.c
index bb54cef..9ae8288 100644
--- a/libinstaller/syslinuxext.c
+++ b/libinstaller/syslinuxext.c
@@ -1,7 +1,178 @@
 #define _GNU_SOURCE
 
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/sysmacros.h>
+#include <getopt.h>
+#include <ext2fs/ext2fs.h>
+
+#include "linuxioctl.h"
+#include "syslinux.h"
+#include "syslxint.h"
+#include "syslxopt.h"
+
+/*
+ * Get the size of a block device
+ */
+static uint64_t get_size(int dev_fd)
+{
+    uint64_t bytes;
+    uint32_t sects;
+    struct stat st;
+
+#ifdef BLKGETSIZE64
+    if (!ioctl(dev_fd, BLKGETSIZE64, &bytes))
+	return bytes;
+#endif
+    if (!ioctl(dev_fd, BLKGETSIZE, &sects))
+	return (uint64_t) sects << 9;
+    else if (!fstat(dev_fd, &st) && st.st_size)
+	return st.st_size;
+    else
+	return 0;
+}
+
+/*
+ * Get device geometry and partition offset
+ */
+static struct geometry_table {
+    uint64_t bytes;
+    struct hd_geometry g;
+};
+
+static int sysfs_get_offset(int dev_fd, unsigned long *start)
+{
+    struct stat st;
+    char sysfs_name[128];
+    FILE *f;
+    int rv;
+
+    if (fstat(dev_fd, &st))
+	return -1;
+
+    if ((size_t)snprintf(sysfs_name, sizeof sysfs_name,
+			 "/sys/dev/block/%u:%u/start",
+			 major(st.st_rdev), minor(st.st_rdev))
+	>= sizeof sysfs_name)
+	return -1;
+
+    f = fopen(sysfs_name, "r");
+    if (!f)
+	return -1;
+
+    rv = fscanf(f, "%lu", start);
+    fclose(f);
+
+    return (rv == 1) ? 0 : -1;
+}
+
+/* Standard floppy disk geometries, plus LS-120.  Zipdisk geometry
+   (x/64/32) is the final fallback.  I don't know what LS-240 has
+   as its geometry, since I don't have one and don't know anyone that does,
+   and Google wasn't helpful... */
+static const struct geometry_table standard_geometries[] = {
+    {360 * 1024, {2, 9, 40, 0}},
+    {720 * 1024, {2, 9, 80, 0}},
+    {1200 * 1024, {2, 15, 80, 0}},
+    {1440 * 1024, {2, 18, 80, 0}},
+    {1680 * 1024, {2, 21, 80, 0}},
+    {1722 * 1024, {2, 21, 80, 0}},
+    {2880 * 1024, {2, 36, 80, 0}},
+    {3840 * 1024, {2, 48, 80, 0}},
+    {123264 * 1024, {8, 32, 963, 0}},	/* LS120 */
+    {0, {0, 0, 0, 0}}
+};
+
+static int get_geometry(int dev_fd, uint64_t totalbytes, struct hd_geometry *geo)
+{
+    struct floppy_struct fd_str;
+    struct loop_info li;
+    struct loop_info64 li64;
+    const struct geometry_table *gp;
+    int rv = 0;
+
+    memset(geo, 0, sizeof *geo);
+
+    if (!ioctl(dev_fd, HDIO_GETGEO, geo)) {
+	goto ok;
+    } else if (!ioctl(dev_fd, FDGETPRM, &fd_str)) {
+	geo->heads = fd_str.head;
+	geo->sectors = fd_str.sect;
+	geo->cylinders = fd_str.track;
+	geo->start = 0;
+	goto ok;
+    }
+
+    /* Didn't work.  Let's see if this is one of the standard geometries */
+    for (gp = standard_geometries; gp->bytes; gp++) {
+	if (gp->bytes == totalbytes) {
+	    memcpy(geo, &gp->g, sizeof *geo);
+	    goto ok;
+	}
+    }
+
+    /* Didn't work either... assign a geometry of 64 heads, 32 sectors; this is
+       what zipdisks use, so this would help if someone has a USB key that
+       they're booting in USB-ZIP mode. */
+
+    geo->heads = opt.heads ? : 64;
+    geo->sectors = opt.sectors ? : 32;
+    geo->cylinders = totalbytes / (geo->heads * geo->sectors << SECTOR_SHIFT);
+    geo->start = 0;
+
+    if (!opt.sectors && !opt.heads) {
+	fprintf(stderr,
+		"Warning: unable to obtain device geometry (defaulting to %d heads, %d sectors)\n"
+		"         (on hard disks, this is usually harmless.)\n",
+		geo->heads, geo->sectors);
+	rv = 1;			/* Suboptimal result */
+    }
+
+ok:
+    /* If this is a loopback device, try to set the start */
+    if (!ioctl(dev_fd, LOOP_GET_STATUS64, &li64))
+	geo->start = li64.lo_offset >> SECTOR_SHIFT;
+    else if (!ioctl(dev_fd, LOOP_GET_STATUS, &li))
+	geo->start = (unsigned int)li.lo_offset >> SECTOR_SHIFT;
+    else if (!sysfs_get_offset(dev_fd, &geo->start)) {
+	/* OK */
+    }
+
+    return rv;
+}
+
+
 /* Patch syslinux_bootsect */
 void syslinux_patch_bootsect(int dev_fd)
 {
+    uint64_t totalbytes, totalsectors;
+    struct hd_geometry geo;
+    struct fat_boot_sector *sbs;
+
+    totalbytes = get_size(dev_fd);
+    get_geometry(dev_fd, totalbytes, &geo);
+
+    if (opt.heads)
+	geo.heads = opt.heads;
+    if (opt.sectors)
+	geo.sectors = opt.sectors;
+
+    /* Patch this into a fake FAT superblock.  This isn't because
+       FAT is a good format in any way, it's because it lets the
+       early bootstrap share code with the FAT version. */
+    sbs = (struct fat_boot_sector *)syslinux_bootsect;
+
+    totalsectors = totalbytes >> SECTOR_SHIFT;
+    if (totalsectors >= 65536) {
+	set_16(&sbs->bsSectors, 0);
+    } else {
+	set_16(&sbs->bsSectors, totalsectors);
+    }
+    set_32(&sbs->bsHugeSectors, totalsectors);
+
+    set_16(&sbs->bsBytesPerSec, SECTOR_SIZE);
+    set_16(&sbs->bsSecPerTrack, geo.sectors);
+    set_16(&sbs->bsHeads, geo.heads);
+    set_32(&sbs->bsHiddenSecs, geo.start);
 }