blob: 0a3296915444826e2836d63d54146c07390b3eca [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001From 1957fc6c069493c6789557936adb675f5e7e51ba 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:43:37 +0800
Patrick Williams92b42cb2022-09-03 06:53:57 -05004Subject: [PATCH] linux/syslinux: add ext_file_read() and ext_file_write()
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
6Will use them to read and write on the extX device.
7
8Upstream-Status: Submitted
9
10Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
11Tested-by: Du Dolpher <dolpher.du@intel.com>
12---
Patrick Williams92b42cb2022-09-03 06:53:57 -050013 linux/syslinux.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014 1 file changed, 62 insertions(+)
15
16diff --git a/linux/syslinux.c b/linux/syslinux.c
Patrick Williams92b42cb2022-09-03 06:53:57 -050017index fc5edb1..c7c1994 100755
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018--- a/linux/syslinux.c
19+++ b/linux/syslinux.c
Patrick Williams92b42cb2022-09-03 06:53:57 -050020@@ -350,6 +350,68 @@ fail:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
22 }
23
24+/* Read from an ext2_file */
25+static int ext_file_read(ext2_file_t e2_file, void *buf, size_t count,
26+ off_t offset, const char *msg)
27+{
28+ int retval;
29+ char *ptr = (char *) buf;
30+ unsigned int got = 0;
31+ size_t done = 0;
32+
33+ /* Always lseek since e2_file is uncontrolled by this func */
34+ if (ext2fs_file_lseek(e2_file, offset, EXT2_SEEK_SET, NULL)) {
35+ fprintf(stderr, "%s: ext2fs_file_lseek() failed.\n",
36+ program);
37+ return -1;
38+ }
39+
40+ while (1) {
41+ retval = ext2fs_file_read(e2_file, ptr, count, &got);
42+ if (retval) {
43+ fprintf(stderr, "%s: error while reading %s\n",
44+ program, msg);
45+ return -1;
46+ }
47+ count -= got;
48+ ptr += got;
49+ done += got;
50+ if (got == 0 || count == 0)
51+ break;
52+ }
53+
54+ return done;
55+}
56+
57+/* Write to an ext2_file */
58+static int ext_file_write(ext2_file_t e2_file, const void *buf, size_t count,
59+ off_t offset)
60+{
61+ const char *ptr = (const char *) buf;
62+ unsigned int written = 0;
63+ size_t done = 0;
64+
65+ /* Always lseek since e2_file is uncontrolled by this func */
66+ if (ext2fs_file_lseek(e2_file, offset, EXT2_SEEK_SET, NULL)) {
67+ fprintf(stderr, "%s: ext2fs_file_lseek() failed.\n",
68+ program);
69+ return -1;
70+ }
71+
72+ while (count > 0) {
73+ if (ext2fs_file_write(e2_file, ptr, count, &written)) {
74+ fprintf(stderr, "%s: failed to write syslinux adv.\n",
75+ program);
76+ return -1;
77+ }
78+ count -= written;
79+ ptr += written;
80+ done += written;
81+ }
82+
83+ return done;
84+}
85+
86 /*
87 * Install the boot block on the specified device.
88 * Must be run AFTER file installed.