[d6b1d73] | 1 | /* |
---|
[9ba0e55] | 2 | * COPYRIGHT (c) 1989-2010. |
---|
| 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
[d6b1d73] | 8 | */ |
---|
| 9 | |
---|
| 10 | #if HAVE_CONFIG_H |
---|
| 11 | #include "config.h" |
---|
| 12 | #endif |
---|
| 13 | |
---|
[9ba0e55] | 14 | /* |
---|
[0ef748fb] | 15 | * This file implements the "mount" procedure for tar-based IMFS |
---|
| 16 | * extensions. The TAR is not actually mounted under the IMFS. |
---|
| 17 | * Directories from the TAR file are created as usual in the IMFS. |
---|
| 18 | * File entries are created as IMFS_LINEAR_FILE nodes with their nods |
---|
| 19 | * pointing to addresses in the TAR image. |
---|
[66b8047] | 20 | */ |
---|
[0ef748fb] | 21 | |
---|
[3b7c123] | 22 | #include "imfs.h" |
---|
[74e0ba5] | 23 | |
---|
[3b7c123] | 24 | #include <sys/stat.h> |
---|
[74e0ba5] | 25 | #include <string.h> |
---|
[3b7c123] | 26 | #include <tar.h> |
---|
[74e0ba5] | 27 | |
---|
[1996aee7] | 28 | #include <rtems/untar.h> |
---|
[0ef748fb] | 29 | |
---|
[9ba0e55] | 30 | /* |
---|
[0ef748fb] | 31 | * TAR file format: |
---|
| 32 | * |
---|
| 33 | * Offset Length Contents |
---|
| 34 | * 0 100 bytes File name ('\0' terminated, 99 maxmum length) |
---|
| 35 | * 100 8 bytes File mode (in octal ascii) |
---|
| 36 | * 108 8 bytes User ID (in octal ascii) |
---|
| 37 | * 116 8 bytes Group ID (in octal ascii) |
---|
| 38 | * 124 12 bytes File size (s) (in octal ascii) |
---|
| 39 | * 136 12 bytes Modify time (in octal ascii) |
---|
| 40 | * 148 8 bytes Header checksum (in octal ascii) |
---|
| 41 | * 156 1 bytes Link flag |
---|
| 42 | * 157 100 bytes Linkname ('\0' terminated, 99 maxmum length) |
---|
[4f3d72f] | 43 | * 257 8 bytes Magic PAX ("ustar\0" + 2 bytes padding) |
---|
| 44 | * 257 8 bytes Magic GNU tar ("ustar \0") |
---|
[0ef748fb] | 45 | * 265 32 bytes User name ('\0' terminated, 31 maxmum length) |
---|
| 46 | * 297 32 bytes Group name ('\0' terminated, 31 maxmum length) |
---|
| 47 | * 329 8 bytes Major device ID (in octal ascii) |
---|
| 48 | * 337 8 bytes Minor device ID (in octal ascii) |
---|
| 49 | * 345 167 bytes Padding |
---|
| 50 | * 512 (s+p)bytes File contents (s+p) := (((s) + 511) & ~511), |
---|
| 51 | * round up to 512 bytes |
---|
[a5305f6b] | 52 | * |
---|
[0ef748fb] | 53 | * Checksum: |
---|
| 54 | * int i, sum; |
---|
| 55 | * char* header = tar_header_pointer; |
---|
| 56 | * sum = 0; |
---|
| 57 | * for(i = 0; i < 512; i++) |
---|
| 58 | * sum += 0xFF & header[i]; |
---|
[9ba0e55] | 59 | */ |
---|
[0ef748fb] | 60 | |
---|
| 61 | #define MAX_NAME_FIELD_SIZE 99 |
---|
| 62 | |
---|
| 63 | #define MIN(a,b) ((a)>(b)?(b):(a)) |
---|
| 64 | |
---|
[9ba0e55] | 65 | /* |
---|
[d1941587] | 66 | * rtems_tarfs_load |
---|
[0ef748fb] | 67 | * |
---|
[d1941587] | 68 | * Here we create the mountpoint directory and load the tarfs at |
---|
[0ef748fb] | 69 | * that node. Once the IMFS has been mounted, we work through the |
---|
| 70 | * tar image and perform as follows: |
---|
| 71 | * - For directories, simply call mkdir(). The IMFS creates nodes as |
---|
| 72 | * needed. |
---|
[a5305f6b] | 73 | * - For files, we make our own calls to IMFS eval_for_make and |
---|
[0ef748fb] | 74 | * create_node. |
---|
[9ba0e55] | 75 | */ |
---|
| 76 | int rtems_tarfs_load( |
---|
[3b7c123] | 77 | const char *mountpoint, |
---|
[eb12bf4] | 78 | uint8_t *tar_image, |
---|
[3b7c123] | 79 | size_t tar_size |
---|
[eb12bf4] | 80 | ) |
---|
[0ef748fb] | 81 | { |
---|
[cb4e992] | 82 | const char *hdr_ptr; |
---|
| 83 | char filename[100]; |
---|
| 84 | char full_filename[256]; |
---|
| 85 | int hdr_chksum; |
---|
| 86 | unsigned char linkflag; |
---|
| 87 | unsigned long file_size; |
---|
| 88 | unsigned long file_mode; |
---|
| 89 | int offset; |
---|
| 90 | unsigned long nblocks; |
---|
| 91 | IMFS_jnode_t *node; |
---|
[3b7c123] | 92 | int rv = 0; |
---|
[2563410] | 93 | int eval_flags = RTEMS_FS_FOLLOW_LINK; |
---|
[3b7c123] | 94 | rtems_filesystem_eval_path_context_t ctx; |
---|
| 95 | rtems_filesystem_location_info_t rootloc; |
---|
| 96 | rtems_filesystem_location_info_t *currentloc = |
---|
| 97 | rtems_filesystem_eval_path_start( &ctx, mountpoint, eval_flags ); |
---|
| 98 | |
---|
| 99 | rtems_filesystem_eval_path_extract_currentloc( &ctx, &rootloc ); |
---|
| 100 | rtems_filesystem_eval_path_set_flags( |
---|
| 101 | &ctx, |
---|
[2563410] | 102 | RTEMS_FS_MAKE | RTEMS_FS_EXCLUSIVE |
---|
[cb4e992] | 103 | ); |
---|
[3b7c123] | 104 | if (rootloc.ops != &IMFS_ops && rootloc.ops != &fifoIMFS_ops) { |
---|
| 105 | rv = -1; |
---|
| 106 | } |
---|
[d1941587] | 107 | |
---|
[9ba0e55] | 108 | /* |
---|
[0ef748fb] | 109 | * Create an IMFS node structure pointing to tar image memory. |
---|
[9ba0e55] | 110 | */ |
---|
[0ef748fb] | 111 | offset = 0; |
---|
[3b7c123] | 112 | while ( rv == 0 ) { |
---|
[cb4e992] | 113 | if (offset + 512 > tar_size) |
---|
| 114 | break; |
---|
| 115 | |
---|
| 116 | /* |
---|
| 117 | * Read a header. |
---|
| 118 | */ |
---|
| 119 | hdr_ptr = (char *) &tar_image[offset]; |
---|
| 120 | offset += 512; |
---|
[4f3d72f] | 121 | if (strncmp(&hdr_ptr[257], "ustar", 5)) |
---|
[cb4e992] | 122 | break; |
---|
| 123 | |
---|
| 124 | strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE); |
---|
| 125 | filename[MAX_NAME_FIELD_SIZE] = '\0'; |
---|
| 126 | |
---|
| 127 | linkflag = hdr_ptr[156]; |
---|
| 128 | file_mode = _rtems_octal2ulong(&hdr_ptr[100], 8); |
---|
| 129 | file_size = _rtems_octal2ulong(&hdr_ptr[124], 12); |
---|
| 130 | hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8); |
---|
| 131 | |
---|
| 132 | if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum) |
---|
| 133 | break; |
---|
| 134 | |
---|
| 135 | /* |
---|
| 136 | * Generate an IMFS node depending on the file type. |
---|
| 137 | * - For directories, just create directories as usual. IMFS |
---|
| 138 | * will take care of the rest. |
---|
| 139 | * - For files, create a file node with special tarfs properties. |
---|
| 140 | */ |
---|
| 141 | if (linkflag == DIRTYPE) { |
---|
| 142 | strcpy(full_filename, mountpoint); |
---|
| 143 | if (full_filename[strlen(full_filename)-1] != '/') |
---|
| 144 | strcat(full_filename, "/"); |
---|
| 145 | strcat(full_filename, filename); |
---|
| 146 | mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
| 147 | } |
---|
| 148 | /* |
---|
| 149 | * Create a LINEAR_FILE node |
---|
| 150 | */ |
---|
| 151 | else if (linkflag == REGTYPE) { |
---|
[3b7c123] | 152 | rtems_filesystem_location_free( currentloc ); |
---|
| 153 | rtems_filesystem_location_clone( currentloc, &rootloc ); |
---|
| 154 | rtems_filesystem_eval_path_set_path( |
---|
| 155 | &ctx, |
---|
| 156 | filename, |
---|
| 157 | strlen( filename ) |
---|
| 158 | ); |
---|
| 159 | rtems_filesystem_eval_path_continue( &ctx ); |
---|
| 160 | |
---|
| 161 | if ( !rtems_filesystem_location_is_null( currentloc ) ) { |
---|
[cb4e992] | 162 | node = IMFS_create_node( |
---|
[3b7c123] | 163 | currentloc, |
---|
| 164 | IMFS_LINEAR_FILE, |
---|
| 165 | rtems_filesystem_eval_path_get_token( &ctx ), |
---|
| 166 | rtems_filesystem_eval_path_get_tokenlen( &ctx ), |
---|
[cb4e992] | 167 | (file_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) | S_IFREG, |
---|
| 168 | NULL |
---|
| 169 | ); |
---|
| 170 | node->info.linearfile.size = file_size; |
---|
| 171 | node->info.linearfile.direct = &tar_image[offset]; |
---|
[0ef748fb] | 172 | } |
---|
| 173 | |
---|
[cb4e992] | 174 | nblocks = (((file_size) + 511) & ~511) / 512; |
---|
| 175 | offset += 512 * nblocks; |
---|
| 176 | } |
---|
| 177 | } |
---|
[3b7c123] | 178 | |
---|
| 179 | rtems_filesystem_location_free( &rootloc ); |
---|
| 180 | rtems_filesystem_eval_path_cleanup( &ctx ); |
---|
| 181 | |
---|
| 182 | return rv; |
---|
[0ef748fb] | 183 | } |
---|
| 184 | |
---|