blob: 1acd9b0b6958552a8bc4c82dd03fefb2c5ff20cf [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001From c6ddb179577dd4c4ea4d1d154f979e90e53d6bf1 Mon Sep 17 00:00:00 2001
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002From: Robert Yang <liezhi.yang@windriver.com>
3Date: Wed, 31 Dec 2014 16:09:18 +0800
Patrick Williams92b42cb2022-09-03 06:53:57 -05004Subject: [PATCH] linux/syslinux: implement open_ext2_fs()
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
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 +-
Patrick Williams92b42cb2022-09-03 06:53:57 -050018 linux/syslinux.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019 2 files changed, 81 insertions(+), 1 deletion(-)
20
21diff --git a/linux/Makefile b/linux/Makefile
Patrick Williams92b42cb2022-09-03 06:53:57 -050022index 5a49d81..67cbbb4 100644
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023--- a/linux/Makefile
24+++ b/linux/Makefile
Patrick Williams92b42cb2022-09-03 06:53:57 -050025@@ -52,7 +52,7 @@ spotless: clean
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 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
Patrick Williams92b42cb2022-09-03 06:53:57 -050035index 1cc276b..f3727ea 100755
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036--- a/linux/syslinux.c
37+++ b/linux/syslinux.c
Patrick Williams92b42cb2022-09-03 06:53:57 -050038@@ -73,6 +73,7 @@
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 #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
Patrick Williams92b42cb2022-09-03 06:53:57 -050046@@ -83,6 +84,9 @@ char *mntpath = NULL; /* Path on which to mount */
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 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);
Patrick Williams92b42cb2022-09-03 06:53:57 -050056@@ -267,6 +271,82 @@ int do_open_file(char *name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 */
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 */