1 // SPDX-License-Identifier: (GPL-2.0 or MIT)
13 #include "flash-defs.h"
14 #include "flash_confs.h"
16 size_t get_name_match(const flash_configs * , const char *);
18 void prepend(FILE *, size_t);
19 void list(const flash_configs *);
20 int main(int argc, char *argv[]){
26 size_t padding = 0x1000; // default offset for nor
29 const flash_configs * configs = get_flash_confs();
30 const flexspi_nor_config_t * config;
31 while ((opt = getopt(argc, argv, "p:n:o:b:lh")) != -1)
37 padding = strtol(optarg, NULL, 0);
39 printf("Invalid value for -%c.\n", opt);
44 strncpy(name,optarg,32);
47 strncpy(fname,optarg,32);
50 bin = strtol(optarg, NULL, 0);
52 printf("Invalid value for -%c.\n", opt);
68 if(!strnlen(fname,32))
69 puts("Need an output file name.");
71 puts("Need a flash config name.");
72 if(!strnlen(name,32) || !strnlen(fname,32))
75 index = get_name_match(configs,name);
77 printf("No match found for %s!\nExiting...\n",name);
80 printf("Match found using %s flash config.\n",configs[index].name);
82 config = &configs[index].config;
84 fnum = fopen(fname,"wb+");
86 printf("Can't open %s for writing!\nExiting...\n",fname);
90 fwrite(config,sizeof(flexspi_nor_config_t),1,fnum);
91 for(size_t i = sizeof(flexspi_nor_config_t); i < padding;i++)
99 void list(const flash_configs * configs){
100 for(size_t i = 0; *configs[i].name;i++)
101 printf("%lu: %s\n",i,configs[i].name);
105 size_t get_name_match(const flash_configs * configs, const char * name){
107 while(*configs[i].name){
108 if(strncmp(configs[i].name,name,32) == 0)
114 void prepend(FILE * fno, size_t offset){
118 uboot = fopen("u-boot.img","r");
120 puts("Can't open u-boot.img for reading!\nExiting...");
124 SPL = fopen("SPL","r");
126 puts("Can't open SPL for reading!\nExiting...");
131 fseek(SPL, 0, SEEK_END);
136 fwrite(buf,sz,1,fno);
138 for(size_t i = ftell(fno); i < offset;i++)
141 fseek(uboot, 0, SEEK_END);
143 buf = realloc(buf, sz);
145 fread(buf,sz,1,uboot);
146 fwrite(buf,sz,1,fno);
154 puts("+--------------------------------------------------------------+");
155 puts("| -h - This message. |");
156 puts("| -o - Output file name. |");
157 puts("| -n - Name of flash config to use, defined in flash_confs.c . |");
158 puts("| -p - Padding offset, other than default (0x1000). |");
159 puts("| -b - Create binary image, and offset to put u-boot.img at. |");
160 puts("| -l - List names of flash defined in flash_confs.c . |");
161 puts("+--------------------------------------------------------------+");