]> Git Repo - imx_flashconf.git/blob - imx-flashconf.c
error checking
[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         const char * fname;
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                                 fname = optarg;
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) == 32)
73                 puts("Flash config name too long.");
74         if(!strnlen(name,32) || !strnlen(fname,32) || strnlen(name,32) == 32)
75                 help(-3);
76
77         index = get_name_match(configs,name);
78         if(index == -1){
79                 printf("No match found for %s!\nExiting...\n",name);
80                 exit(-2);
81         }
82         printf("Match found using %s flash config.\n",configs[index].name);
83
84         config = &configs[index].config;
85
86         fnum = fopen(fname,"wb+");
87         if(!fnum){
88                 printf("Can't open %s for writing!\nExiting...\n",fname);
89                 exit(-1);
90         }
91
92         fwrite(config,sizeof(flexspi_nor_config_t),1,fnum);
93         for(size_t i = sizeof(flexspi_nor_config_t); i < padding;i++)
94                 fputc(0xff,fnum);
95         if(bin)
96                 prepend(fnum,bin);
97         fclose(fnum);
98         puts("Done!");
99         return 0;
100 }
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);
104         exit(0);
105 }
106
107 size_t get_name_match(const flash_configs * configs, const char * name){
108         size_t i = 0;
109         while(*configs[i].name){
110                 if(strncmp(configs[i].name,name,32) == 0)
111                         return i;
112                 i++;
113         }
114         return -1;
115 }
116 void prepend(FILE * fno, size_t offset){
117         FILE * SPL, * uboot;
118         char * buf;
119         size_t sz;
120         uboot = fopen("u-boot.img","r");
121         if(!uboot){
122                 puts("Can't open u-boot.img for reading!\nExiting...");
123                 fclose(fno);
124                 exit(-1);
125         }
126         SPL = fopen("SPL","r");
127         if(!SPL){
128                 puts("Can't open SPL for reading!\nExiting...");
129                 fclose(uboot);
130                 fclose(fno);
131                 exit(-1);
132         }
133         fseek(SPL, 0, SEEK_END);
134         sz = ftell(SPL);
135         buf = malloc(sz);
136         rewind(SPL);
137         fread(buf,sz,1,SPL);
138         fwrite(buf,sz,1,fno);
139
140         for(size_t i = ftell(fno); i < offset;i++)
141                 fputc(0xff,fno);
142
143         fseek(uboot, 0, SEEK_END);
144         sz = ftell(uboot);
145         buf = realloc(buf, sz);
146         rewind(uboot);
147         fread(buf,sz,1,uboot);
148         fwrite(buf,sz,1,fno);
149         free(buf);
150
151         fclose(SPL);
152         fclose(uboot);
153 }
154
155 void help(int code){
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("+--------------------------------------------------------------+");
164         exit(code);
165 }
This page took 0.033458 seconds and 4 git commands to generate.