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