uboot-lantiq: update to v2013.10
[openwrt.git] / package / boot / uboot-lantiq / patches / 0018-tools-lantiq-add-NAND-SPL-support.patch
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
5
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
7
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
12 @@ -14,7 +14,8 @@
13  
14  enum image_types {
15         IMAGE_NONE,
16 -       IMAGE_SFSPL
17 +       IMAGE_SFSPL,
18 +       IMAGE_NANDSPL
19  };
20  
21  /* Lantiq non-volatile bootstrap command IDs */
22 @@ -43,6 +44,8 @@ enum nvb_cmd_flags {
23  struct args {
24         enum image_types type;
25         __u32           entry_addr;
26 +       loff_t          uboot_offset;
27 +       unsigned int    page_size;
28         const char      *uboot_bin;
29         const char      *spl_bin;
30         const char      *out_bin;
31 @@ -50,10 +53,11 @@ struct args {
32  
33  static void usage_msg(const char *name)
34  {
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",
37                 name);
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");
42  }
43  
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))
47                 return IMAGE_SFSPL;
48  
49 +       if (!strncmp(type, "nandspl", 6))
50 +               return IMAGE_NANDSPL;
51 +
52         return IMAGE_NONE;
53  }
54  
55 @@ -73,7 +80,7 @@ static int parse_args(int argc, char *argv[], struct args *arg)
56  
57         memset(arg, 0, sizeof(*arg));
58  
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) {
61                 switch (opt) {
62                 case 'h':
63                         usage_msg(argv[0]);
64 @@ -84,6 +91,12 @@ static int parse_args(int argc, char *argv[], struct args *arg)
65                 case 'e':
66                         arg->entry_addr = strtoul(optarg, NULL, 16);
67                         break;
68 +               case 'x':
69 +                       arg->uboot_offset = strtoul(optarg, NULL, 16);
70 +                       break;
71 +               case 'p':
72 +                       arg->page_size = strtoul(optarg, NULL, 10);
73 +                       break;
74                 case 'u':
75                         arg->uboot_bin = optarg;
76                         break;
77 @@ -114,11 +127,22 @@ static int parse_args(int argc, char *argv[], struct args *arg)
78                 goto parse_error;
79         }
80  
81 -       if (arg->type == IMAGE_SFSPL && !arg->spl_bin) {
82 +       if ((arg->type == IMAGE_SFSPL || arg->type == IMAGE_NANDSPL) &&
83 +               !arg->spl_bin) {
84                 fprintf(stderr, "Missing SPL binary\n");
85                 goto parse_error;
86         }
87  
88 +       if (arg->type == IMAGE_NANDSPL && !arg->uboot_offset) {
89 +               fprintf(stderr, "Missing U-Boot offset\n");
90 +               goto parse_error;
91 +       }
92 +
93 +       if (arg->type == IMAGE_NANDSPL && !arg->page_size) {
94 +               fprintf(stderr, "Missing NAND page size\n");
95 +               goto parse_error;
96 +       }
97 +
98         return 0;
99  
100  parse_error:
101 @@ -174,6 +198,19 @@ static int write_nvb_start_header(int fd, __u32 addr)
102         return write_header(fd, hdr, sizeof(hdr));
103  }
104  
105 +#if 0
106 +static int write_nvb_regcfg_header(int fd, __u32 addr)
107 +{
108 +       __u32 hdr[2];
109 +
110 +       hdr[0] = build_nvb_command(NVB_CMD_REGCFG, NVB_FLAG_SDBG |
111 +                                       NVB_FLAG_DBG);
112 +       hdr[1] = cpu_to_be32(addr);
113 +
114 +       return write_header(fd, hdr, sizeof(hdr));
115 +}
116 +#endif
117 +
118  static int open_input_bin(const char *name, void **ptr, size_t *size)
119  {
120         struct stat sbuf;
121 @@ -238,9 +275,37 @@ static int open_output_bin(const char *name)
122         return fd;
123  }
124  
125 -static int create_sfspl(const struct args *arg)
126 +static int pad_to_offset(int fd, loff_t offset)
127  {
128 -       int out_fd, uboot_fd, spl_fd, ret;
129 +       loff_t pos;
130 +       size_t size;
131 +       ssize_t n;
132 +       __u8 *buf;
133 +
134 +       pos = lseek(fd, 0, SEEK_CUR);
135 +       size = offset - pos;
136 +
137 +       buf = malloc(size);
138 +       if (!buf) {
139 +               fprintf(stderr, "Failed to malloc buffer\n");
140 +               return -1;
141 +       }
142 +
143 +       memset(buf, 0xff, size);
144 +       n = write(fd, buf, size);
145 +       free(buf);
146 +
147 +       if (n != size) {
148 +               fprintf(stderr, "Failed to write pad bytes\n");
149 +               return -1;
150 +       }
151 +
152 +       return 0;
153 +}
154 +
155 +static int create_spl_image(const struct args *arg)
156 +{
157 +       int out_fd, uboot_fd, spl_fd, ret = 0;
158         void *uboot_ptr, *spl_ptr;
159         size_t uboot_size, spl_size;
160  
161 @@ -256,9 +321,22 @@ static int create_sfspl(const struct args *arg)
162         if (0 > uboot_fd)
163                 goto err_uboot;
164  
165 +#if 0
166 +       ret = write_nvb_regcfg_header(out_fd, 0);
167 +       if (ret)
168 +               goto err_write;
169 +#endif
170 +
171         ret = write_nvb_dwnld_header(out_fd, spl_size, arg->entry_addr);
172         if (ret)
173                 goto err_write;
174 +#if 0
175 +       if (arg->page_size) {
176 +               ret = pad_to_offset(out_fd, arg->page_size);
177 +               if (ret)
178 +                       goto err_write;
179 +       }
180 +#endif
181  
182         ret = copy_bin(out_fd, spl_ptr, spl_size);
183         if (ret)
184 @@ -268,16 +346,16 @@ static int create_sfspl(const struct args *arg)
185         if (ret)
186                 goto err_write;
187  
188 +       if (arg->uboot_offset) {
189 +               ret = pad_to_offset(out_fd, arg->uboot_offset);
190 +               if (ret)
191 +                       goto err_write;
192 +       }
193 +
194         ret = copy_bin(out_fd, uboot_ptr, uboot_size);
195         if (ret)
196                 goto err_write;
197  
198 -       close_input_bin(uboot_fd, uboot_ptr, uboot_size);
199 -       close_input_bin(spl_fd, spl_ptr, spl_size);
200 -       close(out_fd);
201 -
202 -       return 0;
203 -
204  err_write:
205         close_input_bin(uboot_fd, uboot_ptr, uboot_size);
206  err_uboot:
207 @@ -285,7 +363,7 @@ err_uboot:
208  err_spl:
209         close(out_fd);
210  err:
211 -       return -1;
212 +       return ret;
213  }
214  
215  int main(int argc, char *argv[])
216 @@ -299,7 +377,8 @@ int main(int argc, char *argv[])
217  
218         switch (arg.type) {
219         case IMAGE_SFSPL:
220 -               ret = create_sfspl(&arg);
221 +       case IMAGE_NANDSPL:
222 +               ret = create_spl_image(&arg);
223                 break;
224         default:
225                 fprintf(stderr, "Image type not implemented\n");
226 -- 
227 1.8.3.2
228