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 --- a/tools/ltq-boot-image.c
9 +++ b/tools/ltq-boot-image.c
19 /* Lantiq non-volatile bootstrap command IDs */
20 @@ -43,6 +44,8 @@ enum nvb_cmd_flags {
22 enum image_types type;
24 + loff_t uboot_offset;
25 + unsigned int page_size;
26 const char *uboot_bin;
29 @@ -50,10 +53,11 @@ struct args {
31 static void usage_msg(const char *name)
33 - fprintf(stderr, "%s: [-h] -t type -e entry-addr -u uboot-bin [-s spl-bin] -o out-bin\n",
34 + 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",
36 fprintf(stderr, " Image types:\n"
37 - " sfspl - SPL + [compressed] U-Boot for SPI flash\n");
38 + " sfspl - SPL + [compressed] U-Boot for SPI flash\n"
39 + " nandspl - SPL + [compressed] U-Boot for NAND flash\n");
42 static enum image_types parse_image_type(const char *type)
43 @@ -64,6 +68,9 @@ static enum image_types parse_image_type
44 if (!strncmp(type, "sfspl", 6))
47 + if (!strncmp(type, "nandspl", 6))
48 + return IMAGE_NANDSPL;
53 @@ -73,7 +80,7 @@ static int parse_args(int argc, char *ar
55 memset(arg, 0, sizeof(*arg));
57 - while ((opt = getopt(argc, argv, "ht:e:u:s:o:")) != -1) {
58 + while ((opt = getopt(argc, argv, "ht:e:x:p:u:s:o:")) != -1) {
62 @@ -84,6 +91,12 @@ static int parse_args(int argc, char *ar
64 arg->entry_addr = strtoul(optarg, NULL, 16);
67 + arg->uboot_offset = strtoul(optarg, NULL, 16);
70 + arg->page_size = strtoul(optarg, NULL, 10);
73 arg->uboot_bin = optarg;
75 @@ -114,11 +127,22 @@ static int parse_args(int argc, char *ar
79 - if (arg->type == IMAGE_SFSPL && !arg->spl_bin) {
80 + if ((arg->type == IMAGE_SFSPL || arg->type == IMAGE_NANDSPL) &&
82 fprintf(stderr, "Missing SPL binary\n");
86 + if (arg->type == IMAGE_NANDSPL && !arg->uboot_offset) {
87 + fprintf(stderr, "Missing U-Boot offset\n");
91 + if (arg->type == IMAGE_NANDSPL && !arg->page_size) {
92 + fprintf(stderr, "Missing NAND page size\n");
99 @@ -174,6 +198,19 @@ static int write_nvb_start_header(int fd
100 return write_header(fd, hdr, sizeof(hdr));
104 +static int write_nvb_regcfg_header(int fd, __u32 addr)
108 + hdr[0] = build_nvb_command(NVB_CMD_REGCFG, NVB_FLAG_SDBG |
110 + hdr[1] = cpu_to_be32(addr);
112 + return write_header(fd, hdr, sizeof(hdr));
116 static int open_input_bin(const char *name, void **ptr, size_t *size)
119 @@ -238,9 +275,37 @@ static int open_output_bin(const char *n
123 -static int create_sfspl(const struct args *arg)
124 +static int pad_to_offset(int fd, loff_t offset)
126 - int out_fd, uboot_fd, spl_fd, ret;
132 + pos = lseek(fd, 0, SEEK_CUR);
133 + size = offset - pos;
135 + buf = malloc(size);
137 + fprintf(stderr, "Failed to malloc buffer\n");
141 + memset(buf, 0xff, size);
142 + n = write(fd, buf, size);
146 + fprintf(stderr, "Failed to write pad bytes\n");
153 +static int create_spl_image(const struct args *arg)
155 + int out_fd, uboot_fd, spl_fd, ret = 0;
156 void *uboot_ptr, *spl_ptr;
157 size_t uboot_size, spl_size;
159 @@ -256,9 +321,22 @@ static int create_sfspl(const struct arg
164 + ret = write_nvb_regcfg_header(out_fd, 0);
169 ret = write_nvb_dwnld_header(out_fd, spl_size, arg->entry_addr);
173 + if (arg->page_size) {
174 + ret = pad_to_offset(out_fd, arg->page_size);
180 ret = copy_bin(out_fd, spl_ptr, spl_size);
182 @@ -268,16 +346,16 @@ static int create_sfspl(const struct arg
186 + if (arg->uboot_offset) {
187 + ret = pad_to_offset(out_fd, arg->uboot_offset);
192 ret = copy_bin(out_fd, uboot_ptr, uboot_size);
196 - close_input_bin(uboot_fd, uboot_ptr, uboot_size);
197 - close_input_bin(spl_fd, spl_ptr, spl_size);
203 close_input_bin(uboot_fd, uboot_ptr, uboot_size);
205 @@ -285,7 +363,7 @@ err_uboot:
213 int main(int argc, char *argv[])
214 @@ -299,7 +377,8 @@ int main(int argc, char *argv[])
218 - ret = create_sfspl(&arg);
219 + case IMAGE_NANDSPL:
220 + ret = create_spl_image(&arg);
223 fprintf(stderr, "Image type not implemented\n");