Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 1 | From 32c17dd886a5b506781b86c6227bb059ce33420b Mon Sep 17 00:00:00 2001 |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 2 | From: Tung Nguyen <tung.nguyen@amperecomputing.com> |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 3 | Date: Sun, 5 Sep 2021 09:22:08 +0000 |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 4 | Subject: [PATCH] flashcp support offset option |
| 5 | |
| 6 | The flashcp command from mtd-utils just support to flash from offset 0. |
| 7 | Add offset option to specify the start offset so that firmware can be |
| 8 | flashed correctly |
| 9 | |
| 10 | Signed-off-by: Tung Nguyen <tung.nguyen@amperecomputing.com> |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 11 | --- |
| 12 | misc-utils/flashcp.c | 50 +++++++++++++++++++++++++++++++++++++++++--- |
| 13 | 1 file changed, 47 insertions(+), 3 deletions(-) |
| 14 | |
| 15 | diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 16 | index 341c210..0a0e75b 100644 |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 17 | --- a/misc-utils/flashcp.c |
| 18 | +++ b/misc-utils/flashcp.c |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 19 | @@ -91,7 +91,7 @@ static NORETURN void showusage(bool error) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 20 | "\n" |
| 21 | "Flash Copy - Written by Abraham van der Merwe <abraham@2d3d.co.za>\n" |
| 22 | "\n" |
| 23 | - "usage: %1$s [ -v | --verbose | -A | --erase-all ] <filename> <device>\n" |
| 24 | + "usage: %1$s [ -v | --verbose | -A | --erase-all ] <filename> <device> <offset>\n" |
| 25 | " %1$s -h | --help\n" |
| 26 | " %1$s -V | --version\n" |
| 27 | "\n" |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 28 | @@ -102,6 +102,7 @@ static NORETURN void showusage(bool error) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 29 | " -V | --version Show version information and exit\n" |
| 30 | " <filename> File which you want to copy to flash\n" |
| 31 | " <device> Flash device to write to (e.g. /dev/mtd0, /dev/mtd1, etc.)\n" |
| 32 | + " <offset> The start offset. Optional, default: 0\n" |
| 33 | "\n", |
| 34 | PROGRAM_NAME); |
| 35 | |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 36 | @@ -156,6 +157,16 @@ static void safe_rewind (int fd,const char *filename) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | +static int safe_rewind_offset(int fd, const char *filename, off_t offset) |
| 41 | +{ |
| 42 | + if (lseek(fd, offset, SEEK_SET) < 0) { |
| 43 | + log_printf(LOG_ERROR, "While seeking to start of %s: %m\n", filename); |
| 44 | + return EXIT_FAILURE; |
| 45 | + } |
| 46 | + |
| 47 | + return EXIT_SUCCESS; |
| 48 | +} |
| 49 | + |
| 50 | /******************************************************************************/ |
| 51 | |
| 52 | static int dev_fd = -1,fil_fd = -1; |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 53 | @@ -169,6 +180,8 @@ static void cleanup (void) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 54 | int main (int argc,char *argv[]) |
| 55 | { |
| 56 | const char *filename = NULL,*device = NULL; |
| 57 | + off_t offset; |
| 58 | + |
| 59 | int i,flags = FLAG_NONE; |
| 60 | ssize_t result; |
| 61 | size_t size,written; |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 62 | @@ -225,6 +238,20 @@ int main (int argc,char *argv[]) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 63 | showusage(true); |
| 64 | } |
| 65 | } |
| 66 | + |
| 67 | + if (optind + 3 == argc) { |
| 68 | + flags |= FLAG_FILENAME; |
| 69 | + filename = argv[optind]; |
| 70 | + DEBUG("Got filename: %s\n", filename); |
| 71 | + |
| 72 | + flags |= FLAG_DEVICE; |
| 73 | + device = argv[optind + 1]; |
| 74 | + DEBUG("Got device: %s\n", device); |
| 75 | + |
| 76 | + offset = strtoul(argv[optind + 2], NULL, 16); |
| 77 | + DEBUG("Got offset: 0x%x\n", offset); |
| 78 | + } |
| 79 | + |
| 80 | if (optind+2 == argc) { |
| 81 | flags |= FLAG_FILENAME; |
| 82 | filename = argv[optind]; |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 83 | @@ -233,6 +260,8 @@ int main (int argc,char *argv[]) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 84 | flags |= FLAG_DEVICE; |
| 85 | device = argv[optind+1]; |
| 86 | DEBUG("Got device: %s\n",device); |
| 87 | + |
| 88 | + offset = 0; |
| 89 | } |
| 90 | |
| 91 | if (flags & FLAG_HELP || device == NULL) |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 92 | @@ -264,6 +293,12 @@ int main (int argc,char *argv[]) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 93 | exit (EXIT_FAILURE); |
| 94 | } |
| 95 | |
| 96 | + /* does offset is out of the mtd */ |
| 97 | + if (offset > mtd.size) { |
| 98 | + log_printf(LOG_ERROR, "%s offset won't fit into %s!\n", offset, device); |
| 99 | + exit(EXIT_FAILURE); |
| 100 | + } |
| 101 | + |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 102 | /* diff block flashcp */ |
| 103 | if (flags & FLAG_PARTITION) |
| 104 | { |
| 105 | @@ -284,6 +319,8 @@ int main (int argc,char *argv[]) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | + /* Erase from the offset */ |
| 110 | + erase.start = offset; |
| 111 | erase.length = (filestat.st_size + mtd.erasesize - 1) / mtd.erasesize; |
| 112 | erase.length *= mtd.erasesize; |
| 113 | } |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 114 | @@ -330,6 +367,12 @@ int main (int argc,char *argv[]) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 115 | size = filestat.st_size; |
| 116 | i = BUFSIZE; |
| 117 | written = 0; |
| 118 | + |
| 119 | + /* if offset is greater than 0 */ |
| 120 | + if (offset) |
| 121 | + if (safe_rewind_offset(dev_fd, device, offset) != EXIT_SUCCESS) |
| 122 | + return EXIT_FAILURE; |
| 123 | + |
| 124 | while (size) |
| 125 | { |
| 126 | if (size < BUFSIZE) i = size; |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 127 | @@ -374,8 +417,9 @@ int main (int argc,char *argv[]) |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 128 | * verify that flash == file data * |
| 129 | **********************************/ |
| 130 | |
| 131 | - safe_rewind (fil_fd,filename); |
| 132 | - safe_rewind (dev_fd,device); |
| 133 | + safe_rewind(fil_fd, filename); |
| 134 | + safe_rewind_offset(dev_fd, device, offset); |
| 135 | + |
| 136 | size = filestat.st_size; |
| 137 | i = BUFSIZE; |
| 138 | written = 0; |
| 139 | -- |
Thang Q. Nguyen | 1ce341f | 2021-09-05 09:23:28 +0000 | [diff] [blame] | 140 | 2.25.1 |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 141 | |