[syslinux:master] extlinux: code cleanup and simplification

syslinux-bot for Nicolas Cornu via Syslinux syslinux at zytor.com
Thu Nov 26 05:03:04 PST 2015


Commit-ID:  82c0ec7b39acb87175ea588f2fa11b21331503c4
Gitweb:     http://www.syslinux.org/commit/82c0ec7b39acb87175ea588f2fa11b21331503c4
Author:     Nicolas Cornu via Syslinux <syslinux at zytor.com>
AuthorDate: Fri, 13 Nov 2015 20:35:29 +0100
Committer:  Paulo Alcantara <pcacjr at zytor.com>
CommitDate: Wed, 25 Nov 2015 21:58:23 -0200

extlinux: code cleanup and simplification

Merge btrfs_read_adv and xfs_read_adv into a single generic function
ext_read_adv and split ext_write_adv_offset out of ext_write_adv.

Use those new functions in rewrite_boot_image and btrfs_install_file
where it is actually hardcoded.

Signed-off-by: Nicolas Cornu <nicolac76 at yahoo.fr>
Signed-off-by: Paulo Alcantara <pcacjr at zytor.com>

---
 extlinux/main.c | 113 +++++++++++++++++++++++++++-----------------------------
 1 file changed, 55 insertions(+), 58 deletions(-)

diff --git a/extlinux/main.c b/extlinux/main.c
index a715963..a7ebd49 100644
--- a/extlinux/main.c
+++ b/extlinux/main.c
@@ -338,6 +338,59 @@ static int patch_file_and_bootblock(int fd, const char *dir, int devfd)
     return rv;
 }
 
+static int ext_read_adv_offset(int devfd, off_t offset)
+{
+    const size_t adv_size = 2 * ADV_SIZE;
+
+    if (xpread(devfd, syslinux_adv, adv_size, offset) != adv_size)
+      return -1;
+
+    return syslinux_validate_adv(syslinux_adv) ? 1 : 0;
+}
+
+static int ext_read_adv(const char *path, int devfd, const char **namep)
+{
+    int err;
+    const char *name;
+
+    if (fs_type == BTRFS) {
+	/* btrfs "ldlinux.sys" is in 64k blank area */
+	return ext_read_adv_offset(devfd, BTRFS_ADV_OFFSET);
+    } else if (fs_type == XFS) {
+	/* XFS "ldlinux.sys" is in the first 2048 bytes of the primary AG */
+	return ext_read_adv_offset(devfd, boot_image_len);
+    } else {
+	err = read_adv(path, name = "ldlinux.sys");
+	if (err == 2)		/* ldlinux.sys does not exist */
+	    err = read_adv(path, name = "extlinux.sys");
+	if (namep)
+	    *namep = name;
+	return err;
+    }
+}
+
+static int ext_write_adv_offset(int devfd, off_t offset)
+{
+    const size_t adv_size = 2 * ADV_SIZE;
+
+    if (xpwrite(devfd, syslinux_adv, adv_size, offset) != adv_size) {
+	perror("writing adv");
+	return 1;
+    }
+
+    return 0;
+}
+
+static int ext_write_adv(const char *path, const char *cfg, int devfd)
+{
+    if (fs_type == BTRFS) {
+	/* btrfs "ldlinux.sys" is in 64k blank area */
+	return ext_write_adv_offset(devfd, BTRFS_ADV_OFFSET);
+    } else {
+	return write_adv(path, cfg);
+    }
+}
+
 /*
  * Install the boot block on the specified device.
  * Must be run AFTER install_file()!
@@ -484,8 +537,7 @@ static int rewrite_boot_image(int devfd, const char *path, const char *filename)
     }
 
     /* Write ADV */
-    ret = xpwrite(fd, syslinux_adv, 2 * ADV_SIZE, boot_image_len);
-    if (ret != 2 * ADV_SIZE) {
+    if (ext_write_adv_offset(fd, boot_image_len)) {
 	fprintf(stderr, "%s: write failure on %s\n", program, filename);
 	goto error;
     }
@@ -617,9 +669,7 @@ int btrfs_install_file(const char *path, int devfd, struct stat *rst)
 	return 1;
     }
     dprintf("write boot_image to 0x%x\n", BTRFS_EXTLINUX_OFFSET);
-    if (xpwrite(devfd, syslinux_adv, 2 * ADV_SIZE, BTRFS_ADV_OFFSET)
-	!= 2 * ADV_SIZE) {
-	perror("writing adv");
+    if (ext_write_adv_offset(devfd, BTRFS_ADV_OFFSET)) {
 	return 1;
     }
     dprintf("write adv to 0x%x\n", BTRFS_ADV_OFFSET);
@@ -1418,59 +1468,6 @@ static int open_device(const char *path, struct stat *st, char **_devname)
     return devfd;
 }
 
-static int btrfs_read_adv(int devfd)
-{
-    if (xpread(devfd, syslinux_adv, 2 * ADV_SIZE, BTRFS_ADV_OFFSET)
-	!= 2 * ADV_SIZE)
-	return -1;
-
-    return syslinux_validate_adv(syslinux_adv) ? 1 : 0;
-}
-
-static inline int xfs_read_adv(int devfd)
-{
-    const size_t adv_size = 2 * ADV_SIZE;
-
-    if (xpread(devfd, syslinux_adv, adv_size, boot_image_len) != adv_size)
-	return -1;
-
-    return syslinux_validate_adv(syslinux_adv) ? 1 : 0;
-}
-
-static int ext_read_adv(const char *path, int devfd, const char **namep)
-{
-    int err;
-    const char *name;
-
-    if (fs_type == BTRFS) {
-	/* btrfs "ldlinux.sys" is in 64k blank area */
-	return btrfs_read_adv(devfd);
-    } else if (fs_type == XFS) {
-	/* XFS "ldlinux.sys" is in the first 2048 bytes of the primary AG */
-	return xfs_read_adv(devfd);
-    } else {
-	err = read_adv(path, name = "ldlinux.sys");
-	if (err == 2)		/* ldlinux.sys does not exist */
-	    err = read_adv(path, name = "extlinux.sys");
-	if (namep)
-	    *namep = name;
-	return err;
-    }
-}
-
-static int ext_write_adv(const char *path, const char *cfg, int devfd)
-{
-    if (fs_type == BTRFS) { /* btrfs "ldlinux.sys" is in 64k blank area */
-	if (xpwrite(devfd, syslinux_adv, 2 * ADV_SIZE,
-		BTRFS_ADV_OFFSET) != 2 * ADV_SIZE) {
-		perror("writing adv");
-		return 1;
-	}
-	return 0;
-    }
-    return write_adv(path, cfg);
-}
-
 static int install_loader(const char *path, int update_only)
 {
     struct stat st, fst;


More information about the Syslinux-commits mailing list