Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | From 35d3842cc4b930c5102eed2921e0189b7f4fd069 Mon Sep 17 00:00:00 2001 |
| 2 | From: Robert Yang <liezhi.yang@windriver.com> |
| 3 | Date: Wed, 31 Dec 2014 16:43:37 +0800 |
| 4 | Subject: [PATCH 4/9] linux/syslinux: add ext_file_read() and ext_file_write() |
| 5 | |
| 6 | Will use them to read and write on the extX device. |
| 7 | |
| 8 | Upstream-Status: Submitted |
| 9 | |
| 10 | Signed-off-by: Robert Yang <liezhi.yang@windriver.com> |
| 11 | Tested-by: Du Dolpher <dolpher.du@intel.com> |
| 12 | --- |
| 13 | linux/syslinux.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 14 | 1 file changed, 62 insertions(+) |
| 15 | |
| 16 | diff --git a/linux/syslinux.c b/linux/syslinux.c |
| 17 | index 45f080d..247c86a 100755 |
| 18 | --- a/linux/syslinux.c |
| 19 | +++ b/linux/syslinux.c |
| 20 | @@ -349,6 +349,68 @@ fail: |
| 21 | |
| 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. |
| 89 | -- |
| 90 | 1.9.1 |
| 91 | |