[syslinux] [PATCH 7/8] extlinux/main.c: implement ext_construct_sectmap_fs()

Robert Yang liezhi.yang at windriver.com
Wed Dec 24 00:16:14 PST 2014


Get the bmap and set the sectors, which is used by the
write_to_device().

Signed-off-by: Robert Yang <liezhi.yang at windriver.com>
---
 extlinux/main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/extlinux/main.c b/extlinux/main.c
index f46ba68..efc6b8b 100644
--- a/extlinux/main.c
+++ b/extlinux/main.c
@@ -1064,10 +1064,59 @@ static int ext_write_to_fs(ext2_file_t e2_file, const void *buf, size_t count,
     return done;
 }
 
+/* The file's block count */
+int block_count = 0;
+static int get_block_count(ext2_filsys fs EXT2FS_ATTR((unused)),
+			     blk64_t *blocknr EXT2FS_ATTR((unused)),
+			     e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+			     blk64_t ref_block EXT2FS_ATTR((unused)),
+			     int ref_offset EXT2FS_ATTR((unused)),
+			     void *private EXT2FS_ATTR((unused)))
+{
+    block_count++;
+    return 0;
+}
+
 /* Construct the boot file map */
 static int ext_construct_sectmap_fs(ext2_filsys fs, ext2_ino_t newino,
                                 sector_t *sectors, int nsect)
 {
+    blk64_t     pblk, blksize, blk = 0;
+    sector_t    sec;
+    int         i, retval;
+
+    blksize = fs->blocksize;
+    blksize >>= SECTOR_SHIFT;
+
+    /* Get the total blocks no. */
+    retval = ext2fs_block_iterate3(fs, newino, BLOCK_FLAG_READ_ONLY,
+            NULL, get_block_count, NULL);
+    if (retval) {
+        fprintf(stderr, "%s: ERROR: ext2fs_block_iterate3() failed.\n", program);
+        return -1;
+    }
+
+    while (nsect) {
+        if (block_count-- == 0)
+            break;
+
+        /* Get the physical block no. (bmap) */
+        retval = ext2fs_bmap2(fs, newino, 0, 0, 0, blk, 0, &pblk);
+        if (retval) {
+            fprintf(stderr, "%s: ERROR: ext2fs_bmap2() failed.\n", program);
+            return -1;
+        }
+
+        blk++;
+        sec = (sector_t)pblk * blksize;
+        for (i = 0; i < blksize; i++) {
+            *sectors++ = sec++;
+            if (! --nsect)
+                break;
+        }
+    }
+
+    return 0;
 }
 
 static int write_to_device(ext2_filsys fs, const char *filename,
-- 
1.9.1



More information about the Syslinux mailing list