]> Git Repo - imx_flashconf.git/blob - imx-flashconf.c
licence rename and list
[imx_flashconf.git] / imx-flashconf.c
1 // SPDX-License-Identifier: (GPL-2.0 or MIT)
2 /*
3  * Copyright (C) 2020 Jesse Taube <[email protected]>
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include <stdbool.h>
13 #include "flash-defs.h"
14 #include "flash_confs.h"
15
16 size_t get_name_match(const flash_configs * , const char *);
17 void help(int);
18 void prepend(FILE *, size_t);
19 void list(const flash_configs *);
20 int main(int argc, char *argv[]){
21
22         char name[32];
23         char fname[32];
24         int opt;
25         size_t index = 0;
26         size_t padding = 0x1000; // default offset for nor
27         size_t bin = 0;
28         FILE * fnum;
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)
32         {
33                 errno = 0;
34                 switch (opt)
35                 {
36                         case 'p':
37                                 padding = strtol(optarg, NULL, 0);
38                                 if (errno) {
39                                         printf("Invalid value for -%c.\n", opt);
40                                         help(-3);
41                                 }
42                                 break;
43                         case 'n':
44                                 strncpy(name,optarg,32);
45                                 break;
46                         case 'o':
47                                 strncpy(fname,optarg,32);
48                                 break;
49                         case 'b':
50                                 bin = strtol(optarg, NULL, 0);
51                                 if (errno) {
52                                         printf("Invalid value for -%c.\n", opt);
53                                         help(-3);
54                                 }
55                                 break;
56                         case 'h':
57                                 help(0);
58                                 break;
59                         case 'l':
60                                 list(configs);
61                                 break;
62                         default:
63                                 help(-3);
64                                 break;
65                 }
66         }
67
68         if(!strnlen(fname,32))
69                 puts("Need an output file name.");
70         if(!strnlen(name,32))
71                 puts("Need a flash config name.");
72         if(!strnlen(name,32) || !strnlen(fname,32))
73                 help(-3);
74
75         index = get_name_match(configs,name);
76         if(index == -1){
77                 printf("No match found for %s!\nExiting...\n",name);
78                 exit(-2);
79         }
80         printf("Match found using %s flash config.\n",configs[index].name);
81
82         config = &configs[index].config;
83
84         fnum = fopen(fname,"wb+");
85         if(!fnum){
86                 printf("Can't open %s for writing!\nExiting...\n",fname);
87                 exit(-1);
88         }
89
90         fwrite(config,sizeof(flexspi_nor_config_t),1,fnum);
91         for(size_t i = sizeof(flexspi_nor_config_t); i < padding;i++)
92                 fputc(0xff,fnum);
93         if(bin)
94                 prepend(fnum,bin);
95         fclose(fnum);
96         puts("Done!");
97         return 0;
98 }
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);
102         exit(0);
103 }
104
105 size_t get_name_match(const flash_configs * configs, const char * name){
106         size_t i = 0;
107         while(*configs[i].name){
108                 if(strncmp(configs[i].name,name,32) == 0)
109                         return i;
110                 i++;
111         }
112         return -1;
113 }
114 void prepend(FILE * fno, size_t offset){
115         FILE * SPL, * uboot;
116         char * buf;
117         size_t sz;
118         uboot = fopen("u-boot.img","r");
119         if(!uboot){
120                 puts("Can't open u-boot.img for reading!\nExiting...");
121                 fclose(fno);
122                 exit(-1);
123         }
124         SPL = fopen("SPL","r");
125         if(!SPL){
126                 puts("Can't open SPL for reading!\nExiting...");
127                 fclose(uboot);
128                 fclose(fno);
129                 exit(-1);
130         }
131         fseek(SPL, 0, SEEK_END);
132         sz = ftell(SPL);
133         buf = malloc(sz);
134         rewind(SPL);
135         fread(buf,sz,1,SPL);
136         fwrite(buf,sz,1,fno);
137
138         for(size_t i = ftell(fno); i < offset;i++)
139                 fputc(0xff,fno);
140
141         fseek(uboot, 0, SEEK_END);
142         sz = ftell(uboot);
143         buf = realloc(buf, sz);
144         rewind(uboot);
145         fread(buf,sz,1,uboot);
146         fwrite(buf,sz,1,fno);
147         free(buf);
148
149         fclose(SPL);
150         fclose(uboot);
151 }
152
153 void help(int code){
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("+--------------------------------------------------------------+");
162         exit(code);
163 }
This page took 0.032238 seconds and 4 git commands to generate.