1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #if HAVE_CONFIG_H |
---|
6 | #include "config.h" |
---|
7 | #endif |
---|
8 | |
---|
9 | /************************************************************************** |
---|
10 | * This file implements the "mount" procedure for tar-based IMFS |
---|
11 | * extensions. The TAR is not actually mounted under the IMFS. |
---|
12 | * Directories from the TAR file are created as usual in the IMFS. |
---|
13 | * File entries are created as IMFS_LINEAR_FILE nodes with their nods |
---|
14 | * pointing to addresses in the TAR image. |
---|
15 | *************************************************************************/ |
---|
16 | |
---|
17 | #include <rtems.h> |
---|
18 | #include <rtems/libio_.h> |
---|
19 | #include <string.h> |
---|
20 | #include <chain.h> |
---|
21 | #include <imfs.h> |
---|
22 | |
---|
23 | |
---|
24 | /************************************************************************** |
---|
25 | * TAR file format: |
---|
26 | * |
---|
27 | * Offset Length Contents |
---|
28 | * 0 100 bytes File name ('\0' terminated, 99 maxmum length) |
---|
29 | * 100 8 bytes File mode (in octal ascii) |
---|
30 | * 108 8 bytes User ID (in octal ascii) |
---|
31 | * 116 8 bytes Group ID (in octal ascii) |
---|
32 | * 124 12 bytes File size (s) (in octal ascii) |
---|
33 | * 136 12 bytes Modify time (in octal ascii) |
---|
34 | * 148 8 bytes Header checksum (in octal ascii) |
---|
35 | * 156 1 bytes Link flag |
---|
36 | * 157 100 bytes Linkname ('\0' terminated, 99 maxmum length) |
---|
37 | * 257 8 bytes Magic ("ustar \0") |
---|
38 | * 265 32 bytes User name ('\0' terminated, 31 maxmum length) |
---|
39 | * 297 32 bytes Group name ('\0' terminated, 31 maxmum length) |
---|
40 | * 329 8 bytes Major device ID (in octal ascii) |
---|
41 | * 337 8 bytes Minor device ID (in octal ascii) |
---|
42 | * 345 167 bytes Padding |
---|
43 | * 512 (s+p)bytes File contents (s+p) := (((s) + 511) & ~511), |
---|
44 | * round up to 512 bytes |
---|
45 | * |
---|
46 | * Checksum: |
---|
47 | * int i, sum; |
---|
48 | * char* header = tar_header_pointer; |
---|
49 | * sum = 0; |
---|
50 | * for(i = 0; i < 512; i++) |
---|
51 | * sum += 0xFF & header[i]; |
---|
52 | *************************************************************************/ |
---|
53 | |
---|
54 | #define LF_OLDNORMAL '\0' /* Normal disk file, Unix compatible */ |
---|
55 | #define LF_NORMAL '0' /* Normal disk file */ |
---|
56 | #define LF_LINK '1' /* Link to previously dumped file */ |
---|
57 | #define LF_SYMLINK '2' /* Symbolic link */ |
---|
58 | #define LF_CHR '3' /* Character special file */ |
---|
59 | #define LF_BLK '4' /* Block special file */ |
---|
60 | #define LF_DIR '5' /* Directory */ |
---|
61 | #define LF_FIFO '6' /* FIFO special file */ |
---|
62 | #define LF_CONFIG '7' /* Contiguous file */ |
---|
63 | |
---|
64 | #define MAX_NAME_FIELD_SIZE 99 |
---|
65 | |
---|
66 | #define MIN(a,b) ((a)>(b)?(b):(a)) |
---|
67 | |
---|
68 | static unsigned long octal2ulong(char *octascii, int len); |
---|
69 | static int compute_tar_header_checksum(char *bufr); |
---|
70 | |
---|
71 | /************************************************************************** |
---|
72 | * rtems_tarfs_load |
---|
73 | * |
---|
74 | * Here we create the mountpoint directory and load the tarfs at |
---|
75 | * that node. Once the IMFS has been mounted, we work through the |
---|
76 | * tar image and perform as follows: |
---|
77 | * - For directories, simply call mkdir(). The IMFS creates nodes as |
---|
78 | * needed. |
---|
79 | * - For files, we make our own calls to IMFS eval_for_make and |
---|
80 | * create_node. |
---|
81 | *************************************************************************/ |
---|
82 | int |
---|
83 | rtems_tarfs_load(char *mountpoint, |
---|
84 | unsigned char *tar_image, |
---|
85 | unsigned long tar_size) |
---|
86 | { |
---|
87 | rtems_filesystem_location_info_t root_loc; |
---|
88 | rtems_filesystem_location_info_t loc; |
---|
89 | char *hdr_ptr; |
---|
90 | char filename[100]; |
---|
91 | char full_filename[256]; |
---|
92 | int hdr_chksum; |
---|
93 | unsigned char linkflag; |
---|
94 | unsigned long file_size; |
---|
95 | unsigned long file_mode; |
---|
96 | int offset; |
---|
97 | unsigned long nblocks; |
---|
98 | IMFS_jnode_t *node; |
---|
99 | int status; |
---|
100 | |
---|
101 | |
---|
102 | status = rtems_filesystem_evaluate_path(mountpoint, 0, &root_loc, 0); |
---|
103 | if (status != 0) |
---|
104 | return(-1); |
---|
105 | |
---|
106 | if (root_loc.ops != &IMFS_ops) |
---|
107 | return(-1); |
---|
108 | |
---|
109 | /*********************************************************************** |
---|
110 | * Create an IMFS node structure pointing to tar image memory. |
---|
111 | **********************************************************************/ |
---|
112 | offset = 0; |
---|
113 | while (1) |
---|
114 | { |
---|
115 | if (offset + 512 > tar_size) |
---|
116 | break; |
---|
117 | |
---|
118 | /****************************************************************** |
---|
119 | * Read a header. |
---|
120 | ******************************************************************/ |
---|
121 | hdr_ptr = &tar_image[offset]; |
---|
122 | offset += 512; |
---|
123 | if (strncmp(&hdr_ptr[257], "ustar ", 7)) |
---|
124 | break; |
---|
125 | |
---|
126 | strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE); |
---|
127 | filename[MAX_NAME_FIELD_SIZE] = '\0'; |
---|
128 | |
---|
129 | linkflag = hdr_ptr[156]; |
---|
130 | file_mode = octal2ulong(&hdr_ptr[100], 8); |
---|
131 | file_size = octal2ulong(&hdr_ptr[124], 12); |
---|
132 | hdr_chksum = (int)octal2ulong(&hdr_ptr[148], 8); |
---|
133 | |
---|
134 | if (compute_tar_header_checksum(hdr_ptr) != hdr_chksum) |
---|
135 | break; |
---|
136 | |
---|
137 | /****************************************************************** |
---|
138 | * Generate an IMFS node depending on the file type. |
---|
139 | * - For directories, just create directories as usual. IMFS |
---|
140 | * will take care of the rest. |
---|
141 | * - For files, create a file node with special tarfs properties. |
---|
142 | *****************************************************************/ |
---|
143 | if (linkflag == LF_DIR) |
---|
144 | { |
---|
145 | strcpy(full_filename, mountpoint); |
---|
146 | if (full_filename[strlen(full_filename)-1] != '/') |
---|
147 | strcat(full_filename, "/"); |
---|
148 | strcat(full_filename, filename); |
---|
149 | mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
150 | } |
---|
151 | /****************************************************************** |
---|
152 | * Create a LINEAR_FILE node if no user write permission. |
---|
153 | *****************************************************************/ |
---|
154 | else if ((linkflag == LF_NORMAL) && |
---|
155 | ((file_mode & 0200) == 0000)) |
---|
156 | { |
---|
157 | const char *name; |
---|
158 | |
---|
159 | loc = root_loc; |
---|
160 | if (IMFS_evaluate_for_make(filename, &loc, &name) == 0) |
---|
161 | { |
---|
162 | node = IMFS_create_node(&loc, |
---|
163 | IMFS_LINEAR_FILE, (char *)name, |
---|
164 | (S_IRUSR | S_IRGRP | S_IROTH) | S_IFREG, |
---|
165 | NULL); |
---|
166 | node->info.linearfile.size = file_size; |
---|
167 | node->info.linearfile.direct = &tar_image[offset]; |
---|
168 | } |
---|
169 | |
---|
170 | nblocks = (((file_size) + 511) & ~511) / 512; |
---|
171 | offset += 512 * nblocks; |
---|
172 | } |
---|
173 | /****************************************************************** |
---|
174 | * Create a regular MEMORY_FILE if write permission exists. |
---|
175 | *****************************************************************/ |
---|
176 | else if ((linkflag == LF_NORMAL) && |
---|
177 | ((file_mode & 0200) == 0200)) |
---|
178 | { |
---|
179 | int fd; |
---|
180 | int n, left, ptr; |
---|
181 | |
---|
182 | strcpy(full_filename, mountpoint); |
---|
183 | if (full_filename[strlen(full_filename)-1] != '/') |
---|
184 | strcat(full_filename, "/"); |
---|
185 | strcat(full_filename, filename); |
---|
186 | |
---|
187 | fd = creat(full_filename, S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP); |
---|
188 | if (fd != -1) |
---|
189 | { |
---|
190 | left = file_size; |
---|
191 | ptr = offset; |
---|
192 | while ((n = write(fd, &tar_image[ptr], left)) > 0) |
---|
193 | { |
---|
194 | left -= n; |
---|
195 | ptr += n; |
---|
196 | } |
---|
197 | close(fd); |
---|
198 | } |
---|
199 | |
---|
200 | nblocks = (((file_size) + 511) & ~511) / 512; |
---|
201 | offset += 512 * nblocks; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | return(status); |
---|
206 | } |
---|
207 | |
---|
208 | /************************************************************************** |
---|
209 | * This converts octal ASCII number representations into an |
---|
210 | * unsigned long. Only support 32-bit numbers for now. |
---|
211 | *************************************************************************/ |
---|
212 | static unsigned long |
---|
213 | octal2ulong(char *octascii, int len) |
---|
214 | { |
---|
215 | int i; |
---|
216 | unsigned long num; |
---|
217 | unsigned long mult; |
---|
218 | |
---|
219 | num = 0; |
---|
220 | mult = 1; |
---|
221 | for (i=len-1; i>=0; i--) |
---|
222 | { |
---|
223 | if ((octascii[i] < '0') || (octascii[i] > '9')) |
---|
224 | continue; |
---|
225 | |
---|
226 | num += mult*((unsigned long)(octascii[i] - '0')); |
---|
227 | mult *= 8; |
---|
228 | } |
---|
229 | return(num); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | /************************************************************************ |
---|
234 | * Compute the TAR checksum and check with the value in |
---|
235 | * the archive. The checksum is computed over the entire |
---|
236 | * header, but the checksum field is substituted with blanks. |
---|
237 | ************************************************************************/ |
---|
238 | static int |
---|
239 | compute_tar_header_checksum(char *bufr) |
---|
240 | { |
---|
241 | int i, sum; |
---|
242 | |
---|
243 | |
---|
244 | sum = 0; |
---|
245 | for (i=0; i<512; i++) |
---|
246 | { |
---|
247 | if ((i >= 148) && (i < 156)) |
---|
248 | sum += 0xff & ' '; |
---|
249 | else |
---|
250 | sum += 0xff & bufr[i]; |
---|
251 | } |
---|
252 | return(sum); |
---|
253 | } |
---|