1 From 43b9a7c9b903302c56d0a1d292a146dbf4de8e49 Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Mon, 12 Aug 2013 01:17:08 +0200
4 Subject: tools: lantiq: add NAND SPL support
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
8 diff --git a/tools/ltq-boot-image.c b/tools/ltq-boot-image.c
9 index 75a188c..743fc6f 100644
10 --- a/tools/ltq-boot-image.c
11 +++ b/tools/ltq-boot-image.c
21 /* Lantiq non-volatile bootstrap command IDs */
22 @@ -43,6 +44,8 @@ enum nvb_cmd_flags {
24 enum image_types type;
26 + loff_t uboot_offset;
27 + unsigned int page_size;
28 const char *uboot_bin;
31 @@ -50,10 +53,11 @@ struct args {
33 static void usage_msg(const char *name)
35 - fprintf(stderr, "%s: [-h] -t type -e entry-addr -u uboot-bin [-s spl-bin] -o out-bin\n",
36 + fprintf(stderr, "%s: [-h] -t type -e entry-addr [-x uboot-offset] [-p page-size] -u uboot-bin [-s spl-bin] -o out-bin\n",
38 fprintf(stderr, " Image types:\n"
39 - " sfspl - SPL + [compressed] U-Boot for SPI flash\n");
40 + " sfspl - SPL + [compressed] U-Boot for SPI flash\n"
41 + " nandspl - SPL + [compressed] U-Boot for NAND flash\n");
44 static enum image_types parse_image_type(const char *type)
45 @@ -64,6 +68,9 @@ static enum image_types parse_image_type(const char *type)
46 if (!strncmp(type, "sfspl", 6))
49 + if (!strncmp(type, "nandspl", 6))
50 + return IMAGE_NANDSPL;
55 @@ -73,7 +80,7 @@ static int parse_args(int argc, char *argv[], struct args *arg)
57 memset(arg, 0, sizeof(*arg));
59 - while ((opt = getopt(argc, argv, "ht:e:u:s:o:")) != -1) {
60 + while ((opt = getopt(argc, argv, "ht:e:x:p:u:s:o:")) != -1) {
64 @@ -84,6 +91,12 @@ static int parse_args(int argc, char *argv[], struct args *arg)
66 arg->entry_addr = strtoul(optarg, NULL, 16);
69 + arg->uboot_offset = strtoul(optarg, NULL, 16);
72 + arg->page_size = strtoul(optarg, NULL, 10);
75 arg->uboot_bin = optarg;
77 @@ -114,11 +127,22 @@ static int parse_args(int argc, char *argv[], struct args *arg)
81 - if (arg->type == IMAGE_SFSPL && !arg->spl_bin) {
82 + if ((arg->type == IMAGE_SFSPL || arg->type == IMAGE_NANDSPL) &&
84 fprintf(stderr, "Missing SPL binary\n");
88 + if (arg->type == IMAGE_NANDSPL && !arg->uboot_offset) {
89 + fprintf(stderr, "Missing U-Boot offset\n");
93 + if (arg->type == IMAGE_NANDSPL && !arg->page_size) {
94 + fprintf(stderr, "Missing NAND page size\n");
101 @@ -174,6 +198,19 @@ static int write_nvb_start_header(int fd, __u32 addr)
102 return write_header(fd, hdr, sizeof(hdr));
106 +static int write_nvb_regcfg_header(int fd, __u32 addr)
110 + hdr[0] = build_nvb_command(NVB_CMD_REGCFG, NVB_FLAG_SDBG |
112 + hdr[1] = cpu_to_be32(addr);
114 + return write_header(fd, hdr, sizeof(hdr));
118 static int open_input_bin(const char *name, void **ptr, size_t *size)
121 @@ -238,9 +275,37 @@ static int open_output_bin(const char *name)
125 -static int create_sfspl(const struct args *arg)
126 +static int pad_to_offset(int fd, loff_t offset)
128 - int out_fd, uboot_fd, spl_fd, ret;
134 + pos = lseek(fd, 0, SEEK_CUR);
135 + size = offset - pos;
137 + buf = malloc(size);
139 + fprintf(stderr, "Failed to malloc buffer\n");
143 + memset(buf, 0xff, size);
144 + n = write(fd, buf, size);
148 + fprintf(stderr, "Failed to write pad bytes\n");
155 +static int create_spl_image(const struct args *arg)
157 + int out_fd, uboot_fd, spl_fd, ret = 0;
158 void *uboot_ptr, *spl_ptr;
159 size_t uboot_size, spl_size;
161 @@ -256,9 +321,22 @@ static int create_sfspl(const struct args *arg)
166 + ret = write_nvb_regcfg_header(out_fd, 0);
171 ret = write_nvb_dwnld_header(out_fd, spl_size, arg->entry_addr);
175 + if (arg->page_size) {
176 + ret = pad_to_offset(out_fd, arg->page_size);
182 ret = copy_bin(out_fd, spl_ptr, spl_size);
184 @@ -268,16 +346,16 @@ static int create_sfspl(const struct args *arg)
188 + if (arg->uboot_offset) {
189 + ret = pad_to_offset(out_fd, arg->uboot_offset);
194 ret = copy_bin(out_fd, uboot_ptr, uboot_size);
198 - close_input_bin(uboot_fd, uboot_ptr, uboot_size);
199 - close_input_bin(spl_fd, spl_ptr, spl_size);
205 close_input_bin(uboot_fd, uboot_ptr, uboot_size);
207 @@ -285,7 +363,7 @@ err_uboot:
215 int main(int argc, char *argv[])
216 @@ -299,7 +377,8 @@ int main(int argc, char *argv[])
220 - ret = create_sfspl(&arg);
221 + case IMAGE_NANDSPL:
222 + ret = create_spl_image(&arg);
225 fprintf(stderr, "Image type not implemented\n");