comparison pirollback/init.c @ 42:15c2ac2c5c73

pirollback: started
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 06 Jul 2013 20:16:34 +0000
parents
children
comparison
equal deleted inserted replaced
41:86a494a5f2b0 42:15c2ac2c5c73
1 #include <sys/types.h>
2 #include <sys/file.h>
3 #include <endian.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <strings.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include "types.h"
10 #include "struct.h"
11
12 u8 blank_flash_line[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
13 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
14
15 char *imgfile;
16 u8 image[0x480000];
17 struct inode_info inode[0x4000];
18 int last_inode;
19
20 static u8 magichdr_first[9] = {'F','f','s','#', 0x10, 0x02, 0xFF, 0xFF, 0xAB};
21 static u8 magichdr_mid[9] = {'F','f','s','#', 0x10, 0x02, 0xFF, 0xFF, 0xBD};
22 static u8 magichdr_last[9] = {'F','f','s','#', 0x10, 0x02, 0xFF, 0xFF, 0xBF};
23
24 read_img_file()
25 {
26 int i, fd, cc;
27 u8 *blk, *compare;
28
29 fd = open(imgfile, O_RDONLY);
30 if (fd < 0) {
31 perror(imgfile);
32 exit(1);
33 }
34 cc = read(fd, image, sizeof image);
35 close(fd);
36 if (cc != sizeof image) {
37 inv: fprintf(stderr,
38 "%s does not appear to be a \"virgin\" Pirelli FFS image\n",
39 imgfile);
40 exit(1);
41 }
42 for (i = 0; i < 18; i++) {
43 blk = image + i * 0x40000;
44 if (i == 0)
45 compare = magichdr_first;
46 else if (i == 17)
47 compare = magichdr_last;
48 else
49 compare = magichdr_mid;
50 if (bcmp(blk, compare, 9))
51 goto inv;
52 }
53 }
54
55 static int
56 convert_ptr(ptr, ino, which)
57 int ptr, ino;
58 char *which;
59 {
60 if (ptr >= 1 && ptr <= 0x3FFF)
61 return(ptr);
62 if (ptr == 0xFFFF)
63 return(0);
64 fprintf(stderr, "error in inode #%x: invalid %s pointer\n", ino, which);
65 exit(1);
66 }
67
68 read_inodes()
69 {
70 int ino;
71 struct inode_flash *fl;
72 struct inode_info *inf;
73
74 for (ino = 1; ino < 0x4000; ino++) {
75 fl = (struct inode_flash *) image + ino;
76 if (!bcmp(fl, blank_flash_line, 16))
77 break;
78 inf = inode + ino;
79 inf->flash = fl;
80 inf->len = le16toh(fl->len);
81 if (!inf->len) {
82 fprintf(stderr, "error: inode %x has zero length\n",
83 ino);
84 exit(1);
85 }
86 if (inf->len & 0xF) {
87 fprintf(stderr, "error: inode %x has bad length\n",
88 ino);
89 exit(1);
90 }
91 inf->type = fl->type;
92 inf->descend = convert_ptr(le16toh(fl->descend), ino,
93 "descendant");
94 inf->sibling = convert_ptr(le16toh(fl->sibling), ino,
95 "sibling");
96 inf->rawloc = le32toh(fl->dataptr);
97 if (inf->rawloc >= 0x48000) {
98 invdptr: fprintf(stderr,
99 "error: inode %x data pointer is out of bounds\n",
100 ino);
101 exit(1);
102 }
103 inf->offset = inf->rawloc << 4;
104 if (inf->offset + inf->len > 0x480000)
105 goto invdptr;
106 inf->dataptr = image + inf->offset;
107 }
108 last_inode = ino - 1;
109 if (!last_inode) {
110 fprintf(stderr, "error: no inodes found!\n");
111 exit(1);
112 }
113 }