blob: 0edae05f8888b5bac2bb50d2113cb7e4a11cbe47 [file] [log] [blame]
Thang Q. Nguyen1ce341f2021-09-05 09:23:28 +00001From 32c17dd886a5b506781b86c6227bb059ce33420b Mon Sep 17 00:00:00 2001
Thang Q. Nguyen509991d2021-07-20 15:48:50 +07002From: Tung Nguyen <tung.nguyen@amperecomputing.com>
Thang Q. Nguyen1ce341f2021-09-05 09:23:28 +00003Date: Sun, 5 Sep 2021 09:22:08 +0000
Thang Q. Nguyen509991d2021-07-20 15:48:50 +07004Subject: [PATCH] flashcp support offset option
5
6The flashcp command from mtd-utils just support to flash from offset 0.
7Add offset option to specify the start offset so that firmware can be
8flashed correctly
9
10Signed-off-by: Tung Nguyen <tung.nguyen@amperecomputing.com>
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070011---
12 misc-utils/flashcp.c | 50 +++++++++++++++++++++++++++++++++++++++++---
13 1 file changed, 47 insertions(+), 3 deletions(-)
14
15diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c
Thang Q. Nguyen1ce341f2021-09-05 09:23:28 +000016index 341c210..0a0e75b 100644
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070017--- a/misc-utils/flashcp.c
18+++ b/misc-utils/flashcp.c
Thang Q. Nguyen1ce341f2021-09-05 09:23:28 +000019@@ -91,7 +91,7 @@ static NORETURN void showusage(bool error)
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070020 "\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. Nguyen1ce341f2021-09-05 09:23:28 +000028@@ -102,6 +102,7 @@ static NORETURN void showusage(bool error)
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070029 " -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. Nguyen1ce341f2021-09-05 09:23:28 +000036@@ -156,6 +157,16 @@ static void safe_rewind (int fd,const char *filename)
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070037 }
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. Nguyen1ce341f2021-09-05 09:23:28 +000053@@ -169,6 +180,8 @@ static void cleanup (void)
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070054 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. Nguyen1ce341f2021-09-05 09:23:28 +000062@@ -225,6 +238,20 @@ int main (int argc,char *argv[])
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070063 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. Nguyen1ce341f2021-09-05 09:23:28 +000083@@ -233,6 +260,8 @@ int main (int argc,char *argv[])
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070084 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. Nguyen1ce341f2021-09-05 09:23:28 +000092@@ -264,6 +293,12 @@ int main (int argc,char *argv[])
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070093 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. Nguyen1ce341f2021-09-05 09:23:28 +0000102 /* diff block flashcp */
103 if (flags & FLAG_PARTITION)
104 {
105@@ -284,6 +319,8 @@ int main (int argc,char *argv[])
Thang Q. Nguyen509991d2021-07-20 15:48:50 +0700106 }
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. Nguyen1ce341f2021-09-05 09:23:28 +0000114@@ -330,6 +367,12 @@ int main (int argc,char *argv[])
Thang Q. Nguyen509991d2021-07-20 15:48:50 +0700115 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. Nguyen1ce341f2021-09-05 09:23:28 +0000127@@ -374,8 +417,9 @@ int main (int argc,char *argv[])
Thang Q. Nguyen509991d2021-07-20 15:48:50 +0700128 * 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. Nguyen1ce341f2021-09-05 09:23:28 +00001402.25.1
Thang Q. Nguyen509991d2021-07-20 15:48:50 +0700141