Root/nandprog/common/loadcfg.c

1/*
2 * Configfile parsing.
3 */
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include <sys/mman.h>
9#include <string.h>
10#include <stdlib.h>
11#include <stdio.h>
12
13#include "include.h"
14
15#define CFG_FIELD_NUM 10
16
17static np_data np;
18
19extern struct nand_oobinfo oob_64[];
20
21const char CFG_FIELD[][30]=
22{
23    "CPUTYPE",
24    "BUSWIDTH",
25    "ROWCYCLES",
26    "PAGESIZE",
27    "PAGEPERBLOCK",
28    "OOBSIZE",
29    "BADBLOCKPOS",
30    "BADBLOCKPAGE",
31    "ECCTYPE",
32    "[END]",
33};
34
35np_data * load_cfg(void)
36{
37    FILE *fp;
38    char line[100];
39    unsigned short i,j;
40    unsigned int d;
41
42    if ((fp = fopen("nandprog.cfg", "r"))==NULL)
43    {
44        printf("Can not open configration file!\n");
45        return 0;
46    }
47    
48    while(!strstr(line, "[NANDPROG]")) //find the nandprog section!
49    {
50        if (feof(fp))
51        {
52            printf("nand programmer configration file illege!\n");
53            return 0;
54        }
55        fscanf(fp,"%s",line);
56    }
57
58    while(1)
59    {
60        if (feof(fp))
61        {
62            printf("nand programmer configration file illege!\n");
63            return 0;
64        }
65        fscanf(fp,"%s",line);
66        if (line[0]==';')
67        {
68            line[0]='\0';
69            continue;
70        }
71
72        for (i=0;i<CFG_FIELD_NUM;i++)
73            if (strstr(line,CFG_FIELD[i])) break;
74        
75        switch (i)
76        {
77        case 0: //CPUTYPE
78            while (!feof(fp))
79            {
80                fscanf(fp,"%s",line);
81                if (strstr(line,"JZ4730"))
82                {
83                    np.pt = JZ4730;
84                    break;
85                }
86                else if (strstr(line,"JZ4740"))
87                {
88                    np.pt = JZ4740;
89                    break;
90                }
91                else continue;
92            }
93            break;
94        case 1: //BUSWIDTH
95            while (!feof(fp))
96            {
97                fscanf(fp,"%d",&d);
98                if (d!=8 && d!=16) continue;
99                np.bw = d;
100                break;
101            }
102            break;
103        case 2: //ROWCYCLES
104            while (!feof(fp))
105            {
106                fscanf(fp,"%d",&d);
107                if (d!=3 && d!=2) continue;
108                np.rc = d;
109                break;
110            }
111            break;
112        case 3: //PAGESIZE
113            while (!feof(fp))
114            {
115                fscanf(fp,"%d",&d);
116                if (d!=2048 && d!=512) continue;
117                np.ps = d;
118                break;
119            }
120            break;
121        case 4: //PAGEPERBLOCK
122            while (!feof(fp))
123            {
124                fscanf(fp,"%d",&d);
125                if (d!=128 && d!=64) continue;
126                np.ppb = d;
127                break;
128            }
129            break;
130        case 5: //OOBSIZE
131            while (!feof(fp))
132            {
133                fscanf(fp,"%d",&d);
134                if (d!=16 && d!=64) continue;
135                np.os = d;
136                break;
137            }
138            break;
139        case 6: //BADBLOCKPOS
140            while (!feof(fp))
141            {
142                fscanf(fp,"%d",&d);
143                if (d>2048) continue;
144                np.bbp = d;
145                break;
146            }
147            break;
148        case 7: //BADBLOCKPAGE
149            while (!feof(fp))
150            {
151                fscanf(fp,"%d",&d);
152                if (d>np.ppb) continue;
153                np.bba = d;
154                break;
155            }
156
157            break;
158        case 8: //ECCTYPE
159            while (!feof(fp))
160            {
161                fscanf(fp,"%s",line);
162                if (strstr(line,"RS"))
163                {
164                    np.et = HARDRS;
165                    d = 36; //36 bytes ecc
166                    oob_64[4].eccbytes = 36;
167                    np.ep = 4;
168                    break;
169                }
170                else if (strstr(line,"HM"))
171                {
172                    np.et = HARDHM;
173                    d = 24; //24 bytes ecc
174                    oob_64[4].eccbytes = 24;
175                    np.ep = 4;
176                    break;
177                }
178                else continue;
179            }
180            while (!feof(fp))
181            {
182                fscanf(fp,"%s",line);
183                if (strstr(line,"{")) break;
184            }
185            for (j = 0;j < d;j++)
186            {
187                if (feof(fp))
188                {
189                    printf("nand programmer configration file illege!\n");
190                    return 0;
191                }
192                fscanf(fp,"%d",&d);
193                if (d > np.os)
194                {
195                    printf("nand programmer configration file illege!\n");
196                    return 0;
197                }
198                oob_64[4].eccpos[j] = d;
199            }
200
201            while (!feof(fp))
202            {
203                fscanf(fp,"%s",line);
204                if (strstr(line,"}")) break;
205            }
206            break;
207        case 9:
208            return &np;
209        default:
210            ;
211        }
212
213    }
214}
215

Archive Download this file



interactive