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);
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) == 32)
73 puts("Flash config name too long.");
74 if(!strnlen(name,32) || !strnlen(fname,32) || strnlen(name,32) == 32)
77 index = get_name_match(configs,name);
79 printf("No match found for %s!\nExiting...\n",name);
82 printf("Match found using %s flash config.\n",configs[index].name);
84 config = &configs[index].config;
86 fnum = fopen(fname,"wb+");
88 printf("Can't open %s for writing!\nExiting...\n",fname);
92 fwrite(config,sizeof(flexspi_nor_config_t),1,fnum);
93 for(size_t i = sizeof(flexspi_nor_config_t); i < padding;i++)
101 void list(const flash_configs * configs){
102 for(size_t i = 0; *configs[i].name;i++)
103 printf("%lu: %s\n",i,configs[i].name);
107 size_t get_name_match(const flash_configs * configs, const char * name){
109 while(*configs[i].name){
110 if(strncmp(configs[i].name,name,32) == 0)
116 void prepend(FILE * fno, size_t offset){
120 uboot = fopen("u-boot.img","r");
122 puts("Can't open u-boot.img for reading!\nExiting...");
126 SPL = fopen("SPL","r");
128 puts("Can't open SPL for reading!\nExiting...");
133 fseek(SPL, 0, SEEK_END);
138 fwrite(buf,sz,1,fno);
140 for(size_t i = ftell(fno); i < offset;i++)
143 fseek(uboot, 0, SEEK_END);
145 buf = realloc(buf, sz);
147 fread(buf,sz,1,uboot);
148 fwrite(buf,sz,1,fno);
156 puts("+--------------------------------------------------------------+");
157 puts("| -h - This message. |");
158 puts("| -o - Output file name. |");
159 puts("| -n - Name of flash config to use, defined in flash_confs.c . |");
160 puts("| -p - Padding offset, other than default (0x1000). |");
161 puts("| -b - Create binary image, and offset to put u-boot.img at. |");
162 puts("| -l - List names of flash defined in flash_confs.c . |");
163 puts("+--------------------------------------------------------------+");