1 From efce87e5ab98664c57e5f4e3955a2f3747df5737 Mon Sep 17 00:00:00 2001
2 From: Robert Yang <liezhi.yang@windriver.com>
3 Date: Fri, 2 Jan 2015 12:26:46 +0800
4 Subject: [PATCH] libinstaller/syslinuxext: implement syslinux_patch_bootsect()
6 Move the related from extlinux/main.c to libinstaller/syslinuxext.c, the
7 syslinux_patch_bootsect() are used by both extlinux/main.c and
10 Upstream-Status: Submitted
12 Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
13 Tested-by: Du Dolpher <dolpher.du@intel.com>
15 Edited to include sysmacros.h
17 Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
19 extlinux/Makefile | 3 +-
20 extlinux/main.c | 167 +-----------------------------------
21 libinstaller/syslinuxext.c | 171 +++++++++++++++++++++++++++++++++++++
22 3 files changed, 176 insertions(+), 165 deletions(-)
24 diff --git a/extlinux/Makefile b/extlinux/Makefile
25 index 1721ee54..62a49728 100644
26 --- a/extlinux/Makefile
27 +++ b/extlinux/Makefile
28 @@ -32,7 +32,8 @@ SRCS = main.c \
29 ../libinstaller/advio.c \
30 ../libinstaller/bootsect_bin.c \
31 ../libinstaller/ldlinuxc32_bin.c \
32 - ../libinstaller/ldlinux_bin.c
33 + ../libinstaller/ldlinux_bin.c \
34 + ../libinstaller/syslinuxext.c
35 OBJS = $(patsubst %.c,%.o,$(notdir $(SRCS)))
37 .SUFFIXES: .c .o .i .s .S
38 diff --git a/extlinux/main.c b/extlinux/main.c
39 index ebff7eae..9add50fb 100644
44 #include "syslxopt.h" /* unified options */
45 #include "mountinfo.h"
46 +#include "syslinuxext.h"
49 # define dprintf printf
51 # define dprintf(...) ((void)0)
54 -#ifndef EXT2_SUPER_OFFSET
55 -#define EXT2_SUPER_OFFSET 1024
58 /* Since we have unused 2048 bytes in the primary AG of an XFS partition,
59 * we will use the first 0~512 bytes starting from 2048 for the Syslinux
61 @@ -93,136 +90,6 @@ static char subvol[BTRFS_SUBVOL_MAX];
62 #define BTRFS_ADV_OFFSET (BTRFS_BOOT_AREA_A_OFFSET + BTRFS_BOOT_AREA_A_SIZE \
66 - * Get the size of a block device
68 -static uint64_t get_size(int devfd)
75 - if (!ioctl(devfd, BLKGETSIZE64, &bytes))
78 - if (!ioctl(devfd, BLKGETSIZE, §s))
79 - return (uint64_t) sects << 9;
80 - else if (!fstat(devfd, &st) && st.st_size)
87 - * Get device geometry and partition offset
89 -struct geometry_table {
91 - struct hd_geometry g;
94 -static int sysfs_get_offset(int devfd, unsigned long *start)
97 - char sysfs_name[128];
101 - if (fstat(devfd, &st))
104 - if ((size_t)snprintf(sysfs_name, sizeof sysfs_name,
105 - "/sys/dev/block/%u:%u/start",
106 - major(st.st_rdev), minor(st.st_rdev))
107 - >= sizeof sysfs_name)
110 - f = fopen(sysfs_name, "r");
114 - rv = fscanf(f, "%lu", start);
117 - return (rv == 1) ? 0 : -1;
120 -/* Standard floppy disk geometries, plus LS-120. Zipdisk geometry
121 - (x/64/32) is the final fallback. I don't know what LS-240 has
122 - as its geometry, since I don't have one and don't know anyone that does,
123 - and Google wasn't helpful... */
124 -static const struct geometry_table standard_geometries[] = {
125 - {360 * 1024, {2, 9, 40, 0}},
126 - {720 * 1024, {2, 9, 80, 0}},
127 - {1200 * 1024, {2, 15, 80, 0}},
128 - {1440 * 1024, {2, 18, 80, 0}},
129 - {1680 * 1024, {2, 21, 80, 0}},
130 - {1722 * 1024, {2, 21, 80, 0}},
131 - {2880 * 1024, {2, 36, 80, 0}},
132 - {3840 * 1024, {2, 48, 80, 0}},
133 - {123264 * 1024, {8, 32, 963, 0}}, /* LS120 */
137 -int get_geometry(int devfd, uint64_t totalbytes, struct hd_geometry *geo)
139 - struct floppy_struct fd_str;
140 - struct loop_info li;
141 - struct loop_info64 li64;
142 - const struct geometry_table *gp;
145 - memset(geo, 0, sizeof *geo);
147 - if (!ioctl(devfd, HDIO_GETGEO, geo)) {
149 - } else if (!ioctl(devfd, FDGETPRM, &fd_str)) {
150 - geo->heads = fd_str.head;
151 - geo->sectors = fd_str.sect;
152 - geo->cylinders = fd_str.track;
157 - /* Didn't work. Let's see if this is one of the standard geometries */
158 - for (gp = standard_geometries; gp->bytes; gp++) {
159 - if (gp->bytes == totalbytes) {
160 - memcpy(geo, &gp->g, sizeof *geo);
165 - /* Didn't work either... assign a geometry of 64 heads, 32 sectors; this is
166 - what zipdisks use, so this would help if someone has a USB key that
167 - they're booting in USB-ZIP mode. */
169 - geo->heads = opt.heads ? : 64;
170 - geo->sectors = opt.sectors ? : 32;
171 - geo->cylinders = totalbytes / (geo->heads * geo->sectors << SECTOR_SHIFT);
174 - if (!opt.sectors && !opt.heads) {
176 - "Warning: unable to obtain device geometry (defaulting to %d heads, %d sectors)\n"
177 - " (on hard disks, this is usually harmless.)\n",
178 - geo->heads, geo->sectors);
179 - rv = 1; /* Suboptimal result */
183 - /* If this is a loopback device, try to set the start */
184 - if (!ioctl(devfd, LOOP_GET_STATUS64, &li64))
185 - geo->start = li64.lo_offset >> SECTOR_SHIFT;
186 - else if (!ioctl(devfd, LOOP_GET_STATUS, &li))
187 - geo->start = (unsigned int)li.lo_offset >> SECTOR_SHIFT;
188 - else if (!sysfs_get_offset(devfd, &geo->start)) {
196 * Query the device geometry and put it into the boot sector.
197 * Map the file and put the map in the boot sector and file.
198 @@ -233,11 +100,8 @@ ok:
199 static int patch_file_and_bootblock(int fd, const char *dir, int devfd)
201 struct stat dirst, xdst;
202 - struct hd_geometry geo;
204 - uint64_t totalbytes, totalsectors;
206 - struct fat_boot_sector *sbs;
207 char *dirpath, *subpath, *xdirpath;
210 @@ -281,33 +145,8 @@ static int patch_file_and_bootblock(int fd, const char *dir, int devfd)
211 /* Now subpath should contain the path relative to the fs base */
212 dprintf("subpath = %s\n", subpath);
214 - totalbytes = get_size(devfd);
215 - get_geometry(devfd, totalbytes, &geo);
218 - geo.heads = opt.heads;
220 - geo.sectors = opt.sectors;
222 - /* Patch this into a fake FAT superblock. This isn't because
223 - FAT is a good format in any way, it's because it lets the
224 - early bootstrap share code with the FAT version. */
225 - dprintf("heads = %u, sect = %u\n", geo.heads, geo.sectors);
227 - sbs = (struct fat_boot_sector *)syslinux_bootsect;
229 - totalsectors = totalbytes >> SECTOR_SHIFT;
230 - if (totalsectors >= 65536) {
231 - set_16(&sbs->bsSectors, 0);
233 - set_16(&sbs->bsSectors, totalsectors);
235 - set_32(&sbs->bsHugeSectors, totalsectors);
237 - set_16(&sbs->bsBytesPerSec, SECTOR_SIZE);
238 - set_16(&sbs->bsSecPerTrack, geo.sectors);
239 - set_16(&sbs->bsHeads, geo.heads);
240 - set_32(&sbs->bsHiddenSecs, geo.start);
241 + /* Patch syslinux_bootsect */
242 + syslinux_patch_bootsect(devfd);
244 /* Construct the boot file map */
246 diff --git a/libinstaller/syslinuxext.c b/libinstaller/syslinuxext.c
247 index bb54cefc..9ae82884 100644
248 --- a/libinstaller/syslinuxext.c
249 +++ b/libinstaller/syslinuxext.c
253 +#include <sys/stat.h>
254 +#include <sys/types.h>
255 +#include <sys/sysmacros.h>
257 +#include <ext2fs/ext2fs.h>
259 +#include "linuxioctl.h"
260 +#include "syslinux.h"
261 +#include "syslxint.h"
262 +#include "syslxopt.h"
265 + * Get the size of a block device
267 +static uint64_t get_size(int dev_fd)
274 + if (!ioctl(dev_fd, BLKGETSIZE64, &bytes))
277 + if (!ioctl(dev_fd, BLKGETSIZE, §s))
278 + return (uint64_t) sects << 9;
279 + else if (!fstat(dev_fd, &st) && st.st_size)
286 + * Get device geometry and partition offset
288 +static struct geometry_table {
290 + struct hd_geometry g;
293 +static int sysfs_get_offset(int dev_fd, unsigned long *start)
296 + char sysfs_name[128];
300 + if (fstat(dev_fd, &st))
303 + if ((size_t)snprintf(sysfs_name, sizeof sysfs_name,
304 + "/sys/dev/block/%u:%u/start",
305 + major(st.st_rdev), minor(st.st_rdev))
306 + >= sizeof sysfs_name)
309 + f = fopen(sysfs_name, "r");
313 + rv = fscanf(f, "%lu", start);
316 + return (rv == 1) ? 0 : -1;
319 +/* Standard floppy disk geometries, plus LS-120. Zipdisk geometry
320 + (x/64/32) is the final fallback. I don't know what LS-240 has
321 + as its geometry, since I don't have one and don't know anyone that does,
322 + and Google wasn't helpful... */
323 +static const struct geometry_table standard_geometries[] = {
324 + {360 * 1024, {2, 9, 40, 0}},
325 + {720 * 1024, {2, 9, 80, 0}},
326 + {1200 * 1024, {2, 15, 80, 0}},
327 + {1440 * 1024, {2, 18, 80, 0}},
328 + {1680 * 1024, {2, 21, 80, 0}},
329 + {1722 * 1024, {2, 21, 80, 0}},
330 + {2880 * 1024, {2, 36, 80, 0}},
331 + {3840 * 1024, {2, 48, 80, 0}},
332 + {123264 * 1024, {8, 32, 963, 0}}, /* LS120 */
336 +static int get_geometry(int dev_fd, uint64_t totalbytes, struct hd_geometry *geo)
338 + struct floppy_struct fd_str;
339 + struct loop_info li;
340 + struct loop_info64 li64;
341 + const struct geometry_table *gp;
344 + memset(geo, 0, sizeof *geo);
346 + if (!ioctl(dev_fd, HDIO_GETGEO, geo)) {
348 + } else if (!ioctl(dev_fd, FDGETPRM, &fd_str)) {
349 + geo->heads = fd_str.head;
350 + geo->sectors = fd_str.sect;
351 + geo->cylinders = fd_str.track;
356 + /* Didn't work. Let's see if this is one of the standard geometries */
357 + for (gp = standard_geometries; gp->bytes; gp++) {
358 + if (gp->bytes == totalbytes) {
359 + memcpy(geo, &gp->g, sizeof *geo);
364 + /* Didn't work either... assign a geometry of 64 heads, 32 sectors; this is
365 + what zipdisks use, so this would help if someone has a USB key that
366 + they're booting in USB-ZIP mode. */
368 + geo->heads = opt.heads ? : 64;
369 + geo->sectors = opt.sectors ? : 32;
370 + geo->cylinders = totalbytes / (geo->heads * geo->sectors << SECTOR_SHIFT);
373 + if (!opt.sectors && !opt.heads) {
375 + "Warning: unable to obtain device geometry (defaulting to %d heads, %d sectors)\n"
376 + " (on hard disks, this is usually harmless.)\n",
377 + geo->heads, geo->sectors);
378 + rv = 1; /* Suboptimal result */
382 + /* If this is a loopback device, try to set the start */
383 + if (!ioctl(dev_fd, LOOP_GET_STATUS64, &li64))
384 + geo->start = li64.lo_offset >> SECTOR_SHIFT;
385 + else if (!ioctl(dev_fd, LOOP_GET_STATUS, &li))
386 + geo->start = (unsigned int)li.lo_offset >> SECTOR_SHIFT;
387 + else if (!sysfs_get_offset(dev_fd, &geo->start)) {
395 /* Patch syslinux_bootsect */
396 void syslinux_patch_bootsect(int dev_fd)
398 + uint64_t totalbytes, totalsectors;
399 + struct hd_geometry geo;
400 + struct fat_boot_sector *sbs;
402 + totalbytes = get_size(dev_fd);
403 + get_geometry(dev_fd, totalbytes, &geo);
406 + geo.heads = opt.heads;
408 + geo.sectors = opt.sectors;
410 + /* Patch this into a fake FAT superblock. This isn't because
411 + FAT is a good format in any way, it's because it lets the
412 + early bootstrap share code with the FAT version. */
413 + sbs = (struct fat_boot_sector *)syslinux_bootsect;
415 + totalsectors = totalbytes >> SECTOR_SHIFT;
416 + if (totalsectors >= 65536) {
417 + set_16(&sbs->bsSectors, 0);
419 + set_16(&sbs->bsSectors, totalsectors);
421 + set_32(&sbs->bsHugeSectors, totalsectors);
423 + set_16(&sbs->bsBytesPerSec, SECTOR_SIZE);
424 + set_16(&sbs->bsSecPerTrack, geo.sectors);
425 + set_16(&sbs->bsHeads, geo.heads);
426 + set_32(&sbs->bsHiddenSecs, geo.start);