summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/syslinux/syslinux/0006-linux-syslinux-implement-write_to_ext-and-add-syslin.patch
blob: cba87252a58ce6a0e4f4a3f3319a318af121cf97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
From 922e56c10e36d876777580c84daef9a66bea6525 Mon Sep 17 00:00:00 2001
From: Robert Yang <liezhi.yang@windriver.com>
Date: Wed, 31 Dec 2014 17:20:43 +0800
Subject: [PATCH 6/9] linux/syslinux: implement write_to_ext() and add
 syslinuxext.c

* The write_to_ext() write file to the extX device, and handle the boot
  sector.
* The syslinuxext.c is used for placing the code which are used by
  extlinux and syslinux (which is syslinux_patch_bootsect()).

Upstream-Status: Submitted

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Tested-by: Du Dolpher <dolpher.du@intel.com>
---
 libinstaller/syslinuxext.c |   7 +++
 libinstaller/syslinuxext.h |   5 ++
 linux/Makefile             |   3 +-
 linux/syslinux.c           | 118 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 libinstaller/syslinuxext.c
 create mode 100644 libinstaller/syslinuxext.h

diff --git a/libinstaller/syslinuxext.c b/libinstaller/syslinuxext.c
new file mode 100644
index 0000000..bb54cef
--- /dev/null
+++ b/libinstaller/syslinuxext.c
@@ -0,0 +1,7 @@
+#define _GNU_SOURCE
+
+/* Patch syslinux_bootsect */
+void syslinux_patch_bootsect(int dev_fd)
+{
+}
+
diff --git a/libinstaller/syslinuxext.h b/libinstaller/syslinuxext.h
new file mode 100644
index 0000000..8abd8b9
--- /dev/null
+++ b/libinstaller/syslinuxext.h
@@ -0,0 +1,5 @@
+#ifndef EXT2_SUPER_OFFSET
+#define EXT2_SUPER_OFFSET 1024
+#endif
+
+void syslinux_patch_bootsect(int dev_fd);
diff --git a/linux/Makefile b/linux/Makefile
index ac1ac58..3b23867 100644
--- a/linux/Makefile
+++ b/linux/Makefile
@@ -30,7 +30,8 @@ SRCS     = syslinux.c \
            ../libinstaller/syslxmod.c \
 	   ../libinstaller/bootsect_bin.c \
 	   ../libinstaller/ldlinuxc32_bin.c \
-	   ../libinstaller/ldlinux_bin.c
+	   ../libinstaller/ldlinux_bin.c \
+	   ../libinstaller/syslinuxext.c
 OBJS	 = $(patsubst %.c,%.o,$(notdir $(SRCS)))
 
 .SUFFIXES: .c .o .i .s .S
diff --git a/linux/syslinux.c b/linux/syslinux.c
index de5d272..f0c97a8 100755
--- a/linux/syslinux.c
+++ b/linux/syslinux.c
@@ -46,6 +46,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/mount.h>
+#include <time.h>
 
 #include "linuxioctl.h"
 
@@ -72,6 +73,7 @@
 #include "syslxfs.h"
 #include "setadv.h"
 #include "syslxopt.h" /* unified options */
+#include "syslinuxext.h"
 #include <ext2fs/ext2fs.h>
 
 extern const char *program;	/* Name of program */
@@ -419,6 +421,12 @@ int install_bootblock(int fd, const char *device)
 {
 }
 
+/* Construct the boot file map */
+int ext_construct_sectmap_fs(ext2_filsys fs, ext2_ino_t newino,
+                                sector_t *sectors, int nsect)
+{
+}
+
 static int handle_adv_on_ext(void)
 {
     int                 i, retval, found_file;
@@ -524,6 +532,116 @@ fail:
 static int write_to_ext(const char *filename, const char *str, int length,
                         int i_flags, int dev_fd, const char *subdir)
 {
+    ext2_ino_t          newino;
+    struct ext2_inode   inode;
+    int                 retval, i, modbytes, nsect;
+    ext2_file_t         e2_file;
+    sector_t            *sectors;
+
+    /* Remove it if it is already exists */
+    retval = ext2fs_namei(e2fs, root, cwd, filename, &newino);
+    if (retval == 0) {
+        retval = ext2fs_unlink(e2fs, cwd, filename, newino, 0);
+        if (retval) {
+            fprintf(stderr, "%s: failed to unlink: %s\n", program, filename);
+            return retval;
+        }
+    }
+
+    /* Create new inode */
+    retval = ext2fs_new_inode(e2fs, cwd, 010755, 0, &newino);
+    if (retval) {
+        fprintf(stderr, "%s: ERROR: failed to create inode for: %s\n",
+                program, filename);
+        return retval;
+    }
+
+    /* Link the inode and the filename */
+    retval = ext2fs_link(e2fs, cwd, filename, newino, EXT2_FT_REG_FILE);
+    if (retval) {
+        fprintf(stderr, "%s: ERROR: failed to link inode for: %s.\n",
+                program, filename);
+        return retval;
+    }
+
+    if (ext2fs_test_inode_bitmap2(e2fs->inode_map, newino))
+       fprintf(stderr, "%s: warning: inode already set %s.\n",
+            program, filename);
+
+        ext2fs_inode_alloc_stats2(e2fs, newino, +1, 0);
+        memset(&inode, 0, sizeof(inode));
+	inode.i_mode = LINUX_S_IFREG | LINUX_S_IRUSR | LINUX_S_IRGRP
+                        | LINUX_S_IROTH;
+	inode.i_flags |= i_flags;
+        inode.i_atime = inode.i_ctime = inode.i_mtime =
+            e2fs->now ? e2fs->now : time(0);
+        inode.i_links_count = 1;
+        if (e2fs->super->s_feature_incompat &
+            EXT3_FEATURE_INCOMPAT_EXTENTS) {
+            struct ext3_extent_header *eh;
+
+            eh = (struct ext3_extent_header *) &inode.i_block[0];
+            eh->eh_depth = 0;
+            eh->eh_entries = 0;
+            eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
+            i = (sizeof(inode.i_block) - sizeof(*eh)) /
+                sizeof(struct ext3_extent);
+            eh->eh_max = ext2fs_cpu_to_le16(i);
+            inode.i_flags |= EXT4_EXTENTS_FL;
+    }
+
+    retval = ext2fs_write_new_inode(e2fs, newino, &inode);
+    if (retval) {
+        fprintf(stderr, "%s: ERROR: while writting inode %d.\n",
+                program, newino);
+        return 1;
+    }
+
+    retval = ext2fs_file_open(e2fs, newino, EXT2_FILE_WRITE, &e2_file);
+    if (retval) {
+        fprintf(stderr, "%s: ERROR: failed to open %s.\n",
+                program, filename);
+        return 1;
+    }
+
+    /* Write to file */
+    if (ext_file_write(e2_file, str, length, 0) == -1)
+        goto fail;
+
+    if (strcmp(filename, "ldlinux.sys") == 0) {
+        /* Write ADV */
+        if (ext_file_write(e2_file, syslinux_adv, 2 * ADV_SIZE,
+                boot_image_len) == -1)
+            goto fail;
+
+        /* Patch syslinux_bootsect */
+        syslinux_patch_bootsect(dev_fd);
+
+        /* Patch ldlinux.sys */
+        nsect = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
+        nsect += 2;                        /* Two sectors for the ADV */
+        sectors = alloca(sizeof(sector_t) * nsect);
+        memset(sectors, 0, nsect * sizeof *sectors);
+        /* The sectors will be modified and used by syslinux_patch() */
+        retval = ext_construct_sectmap_fs(e2fs, newino, sectors, nsect);
+        if (retval)
+            goto fail;
+
+        /* Create the modified image in memory */
+        modbytes = syslinux_patch(sectors, nsect, opt.stupid_mode,
+                            opt.raid_mode, subdir, NULL);
+
+        /* Rewrite the first modbytes of ldlinux.sys */
+        if (ext_file_write(e2_file, str, modbytes, 0) == -1) {
+            fprintf(stderr, "%s: ERROR: failed to patch %s.\n", program,
+                    filename);
+            goto fail;
+        }
+    }
+
+fail:
+    (void) ext2fs_file_close(e2_file);
+    return retval;
 }
 
 /* The install func for ext2, ext3 and ext4 */
-- 
1.9.1