1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
14 #include <sys/types.h>
22 static int inbetween_white(char *s, int max, char **start, char **end,
24 static int add_header(struct ffs_chain_t *, struct ffs_header_t *);
26 static int glob_come_from_cr = 0;
29 find_next_entry(int file, struct ffs_chain_t *chain)
31 #define MAX_LINE_SIZE 1024
32 char lnbuf[MAX_LINE_SIZE], b0 = 0, b1 = 0;
33 char *start, *end, *next;
34 struct ffs_header_t *hdr; //, *hdr2;
38 /* search for new config line */
39 if (0 == glob_come_from_cr) {
40 while (1 == (rc = read(file, &c, 1))) {
41 //printf("b0=%c b1=%c c=%c\n",
45 /* this looks for starting sign "<CR>[^#]" */
46 if (((0x0a == b0) || (0x0d == b0)) &&
47 (('#' != b1) && (0x0a != b1) && (0x0d != b1))) {
53 while (1 == (rc = read(file, &c, 1))) {
54 //printf("read c=%c\n", c);
55 if ((0x0a != c) && (0x0d != c)) {
59 glob_come_from_cr = 0;
60 //printf("debug: glob_come_from_cr = 0\n");
66 /* now buffer it until end of line */
67 memset((void *) lnbuf, 0, MAX_LINE_SIZE);
70 while ((1 == read(file, &(lnbuf[lc]), 1)) && (lc < MAX_LINE_SIZE)) {
71 //printf("read lnbuf=%c\n", lnbuf[lc]);
72 if ((0x0a == lnbuf[lc]) || (0x0d == lnbuf[lc])) {
73 glob_come_from_cr = 1;
74 //printf("debug: glob_come_from_cr = 1\n");
81 hdr = malloc(sizeof(struct ffs_header_t));
83 perror("alloc memory");
86 memset((void *) hdr, 0, sizeof(struct ffs_header_t));
88 /* attach header to chain */
89 if (0 != add_header(chain, hdr)) {
93 /**********************************************************/
94 /* extract token name *********************************** */
96 if (inbetween_white(lnbuf, MAX_LINE_SIZE, &start, &end, &next) != 0) {
97 printf("parsing error 1");
100 /* get memory for it */
101 hdr->token = malloc(end - start + 1);
102 if (NULL == hdr->token) {
106 strncpy(hdr->token, start, end - start + 1);
107 hdr->token[end - start] = 0;
109 /**********************************************************/
110 /* extract file name *********************************** */
115 if (inbetween_white(lnbuf, MAX_LINE_SIZE, &start, &end, &next) != 0) {
116 printf("parsing error 1");
120 /* get memory for it */
121 hdr->imagefile = malloc(end - start + 1);
122 if (NULL == hdr->imagefile) {
126 /* check if file is existing */
129 strncpy(hdr->imagefile, start, end - start + 1);
130 hdr->imagefile[end - start] = 0;
132 /* check if entry is linked to another header */
135 ("\nERROR: links are removed as feature in this version\n");
140 if (0 != find_entry_by_token(chain, hdr->imagefile+1, &hdr2)) {
141 printf("[%s]: link to [%s] not found\n",
142 hdr->token, hdr->imagefile+1);
143 dump_fs_contents(chain);
146 hdr->linked_to = hdr2;
150 /**********************************************************/
151 /* extract flags name *********************************** */
156 if (inbetween_white(lnbuf, MAX_LINE_SIZE, &start, &end, &next) != 0) {
157 printf("parsing error 1");
160 hdr->flags = strtoul(start, NULL, 16);
162 /**********************************************************/
163 /* extract rom start name *********************************** */
168 if (inbetween_white(lnbuf, MAX_LINE_SIZE, &start, &end, &next) != 0) {
169 printf("parsing error 1");
173 /* this means not specific address request for data */
176 /* data has to begin at specific address */
177 hdr->romaddr = strtoul(start, NULL, 16);
184 read_config(int conf_file, struct ffs_chain_t *ffs_chain)
189 rc = find_next_entry(conf_file, ffs_chain);
197 inbetween_white(char *s, int max, char **start, char **end, char **next)
201 if (NULL != *start) {
206 /* wind to first non white */
208 if ((' ' == *s) || (' ' == *s)) {
216 /* no non-white found */
223 /* wind to end of non white or end of buffer */
226 if ((' ' == *s) || (' ' == *s) ||
227 (0x0a == *s) || (0x0d == *s)) {
240 if ((pos + 1) >= max) {
250 add_header(struct ffs_chain_t *chain, struct ffs_header_t *hdr)
252 struct ffs_header_t *next;
254 if (NULL == chain->first) {
262 while (NULL != next->next) {
272 dump_fs_contents(struct ffs_chain_t *chain)
274 struct ffs_header_t *next;
276 if (NULL == chain->first) {
277 printf("no contents in fs\n");
283 if (NULL != next->token) {
284 printf("Token [%s] ", next->token);
286 printf(" [not-set], ");
289 if (NULL != next->imagefile) {
290 printf(" <%s>, ", next->imagefile);
292 printf(" file<not-set>, ");
295 printf("flags<%llx>, ", next->flags);
296 printf("romaddr<%llx>, ", next->romaddr);
298 if (NULL != next->linked_to) {
299 printf("linked to [%s]", next->linked_to->token);
303 if (NULL == next->next) {
313 free_chain_memory(struct ffs_chain_t *chain)
315 struct ffs_header_t *hdr, *next_hdr;
317 if (NULL != chain->first) {
324 while (NULL != hdr) {
325 //printf("%p ", hdr);
326 if (NULL != hdr->token) {
327 //printf("free up %s\n", hdr->token);
330 if (NULL != hdr->imagefile) {
331 free(hdr->imagefile);
333 next_hdr = hdr->next;
341 * Detect duplicate entries in the romfs list
344 find_duplicates(struct ffs_chain_t *chain)
346 struct ffs_header_t *act, *sub;
348 if (NULL == chain->first) {
349 printf("no contents in fs\n");
356 while (sub != NULL) {
358 if (act->token == NULL || sub->token == NULL) {
359 printf("find_duplicates: token not set!\n");
360 } else if (strcmp(act->token, sub->token) == 0) {
361 printf("*** NOTE: duplicate romfs file '%s'.\n",
369 } while (act != NULL);