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