1From 07fb737fb60c08eaaa41989d531fc23009523546 Mon Sep 17 00:00:00 2001
2From: Robert Yang <liezhi.yang@windriver.com>
3Date: Wed, 31 Dec 2014 16:09:18 +0800
4Subject: [PATCH 2/9] linux/syslinux: implement open_ext2_fs()
5
6The open_ext2_fs() checks whether it is an ext2/ext3/ext4 device, and
7return:
80: It is an ext2, ext3 or ext4.
91: Not an ext2, ext3 or ext4.
10-1: unexpected error.
11
12Upstream-Status: Submitted
13
14Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
15Tested-by: Du Dolpher <dolpher.du@intel.com>
16---
17 linux/Makefile   |  2 +-
18 linux/syslinux.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 2 files changed, 81 insertions(+), 1 deletion(-)
20
21diff --git a/linux/Makefile b/linux/Makefile
22index 11667e1..ac1ac58 100644
23--- a/linux/Makefile
24+++ b/linux/Makefile
25@@ -51,7 +51,7 @@ spotless: clean
26 installer: syslinux syslinux-nomtools
27
28 syslinux: $(OBJS)
29-	$(CC) $(LDFLAGS) -o $@ $^
30+	$(CC) $(LDFLAGS) -o $@ $^ -lext2fs
31
32 syslinux-nomtools: syslinux
33 	ln -f $< $@
34diff --git a/linux/syslinux.c b/linux/syslinux.c
35index 36fc202..cc4e7da 100755
36--- a/linux/syslinux.c
37+++ b/linux/syslinux.c
38@@ -72,6 +72,7 @@
39 #include "syslxfs.h"
40 #include "setadv.h"
41 #include "syslxopt.h" /* unified options */
42+#include <ext2fs/ext2fs.h>
43
44 extern const char *program;	/* Name of program */
45
46@@ -82,6 +83,9 @@ char *mntpath = NULL;		/* Path on which to mount */
47 int loop_fd = -1;		/* Loop device */
48 #endif
49
50+ext2_filsys     e2fs = NULL;    /* Ext2/3/4 filesystem */
51+ext2_ino_t      root, cwd;      /* The root and cwd of e2fs */
52+
53 void __attribute__ ((noreturn)) die(const char *msg)
54 {
55     fprintf(stderr, "%s: %s\n", program, msg);
56@@ -266,6 +270,82 @@ int do_open_file(char *name)
57  */
58 static int open_ext2_fs(const char *device, const char *subdir)
59 {
60+    int         retval;
61+    int         open_flag = EXT2_FLAG_RW, mount_flags;
62+    ext2_ino_t  dirino;
63+    char        opt_string[40];
64+
65+    if (opt.offset) {
66+        sprintf(opt_string, "offset=%llu", (unsigned long long)opt.offset);
67+        retval = ext2fs_open2(device, opt_string, open_flag, 0, 0, unix_io_manager, &e2fs);
68+    } else
69+        retval = ext2fs_open(device, open_flag, 0, 0, unix_io_manager, &e2fs);
70+    if (retval) {
71+        /* It might not be an extN fs, so we need check magic firstly */
72+        if (retval == EXT2_ET_BAD_MAGIC) {
73+            /* Do nothing, return silently */
74+            return 1;
75+        } else {
76+            fprintf(stderr, "%s: error while trying to open: %s\n",
77+                program, device);
78+            return -1;
79+        }
80+    }
81+
82+    /* Stop if it is mounted */
83+    retval = ext2fs_check_if_mounted(device, &mount_flags);
84+    if (retval) {
85+        fprintf(stderr, "%s: ext2fs_check_if_mount() error on %s\n",
86+                program, device);
87+        goto fail;
88+    }
89+
90+    if (mount_flags & EXT2_MF_MOUNTED) {
91+        fprintf(stderr, "%s: %s is mounted\n", program, device);
92+        goto fail;
93+    }
94+
95+    e2fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
96+
97+    /* Read the inode map */
98+    retval = ext2fs_read_inode_bitmap(e2fs);
99+    if (retval) {
100+        fprintf(stderr, "%s: while reading inode bitmap: %s\n",
101+                program, device);
102+        goto fail;
103+    }
104+
105+    /* Read the block map */
106+    retval = ext2fs_read_block_bitmap(e2fs);
107+    if (retval) {
108+        fprintf(stderr, "%s: while reading block bitmap: %s\n",
109+                program, device);
110+        goto fail;
111+    }
112+
113+    root = cwd = EXT2_ROOT_INO;
114+    /* Check the subdir */
115+    if (strcmp(subdir, "/")) {
116+	retval = ext2fs_namei(e2fs, root, cwd, subdir, &dirino);
117+        if (retval) {
118+            fprintf(stderr, "%s: failed to find dir %s on %s\n",
119+                program, subdir, device);
120+            goto fail;
121+        }
122+
123+        retval = ext2fs_check_directory(e2fs, dirino);
124+        if (retval) {
125+            fprintf(stderr, "%s: failed to cd to: %s\n", program, subdir);
126+                goto fail;
127+        }
128+        cwd = dirino;
129+    }
130+
131+    return 0;
132+
133+fail:
134+    (void) ext2fs_close(e2fs);
135+    return -1;
136 }
137
138 /* The install func for ext2, ext3 and ext4 */
139--
1401.9.1
141
142