diff options
-rw-r--r-- | common/environment.c | 62 | ||||
-rw-r--r-- | include/cramfs/cramfs_fs.h | 4 | ||||
-rw-r--r-- | include/envfs.h | 18 |
3 files changed, 53 insertions, 31 deletions
diff --git a/common/environment.c b/common/environment.c index 67ede38347..6e414d9750 100644 --- a/common/environment.c +++ b/common/environment.c @@ -67,9 +67,9 @@ int file_save_action(const char *filename, struct stat *statbuf, statbuf->st_size, namelen); inode = (struct envfs_inode*)data->writep; - inode->magic = ENVFS_INODE_MAGIC; - inode->namelen = namelen; - inode->size = statbuf->st_size; + inode->magic = ENVFS_32(ENVFS_INODE_MAGIC); + inode->namelen = ENVFS_32(namelen); + inode->size = ENVFS_32(statbuf->st_size); data->writep += sizeof(struct envfs_inode); strcpy(data->writep, filename + strlen(data->base)); @@ -122,15 +122,15 @@ int envfs_save(char *filename, char *dirname) data.writep = buf + sizeof(struct envfs_super); super = (struct envfs_super *)buf; - super->magic = ENVFS_MAGIC; - super->size = size; + super->magic = ENVFS_32(ENVFS_MAGIC); + super->size = ENVFS_32(size); /* second pass: copy files to buffer */ recursive_action(dirname, ACTION_RECURSE, file_save_action, NULL, &data, 0); - super->crc = crc32(0, buf + sizeof(struct envfs_super), size); - super->sb_crc = crc32(0, buf, sizeof(struct envfs_super) - 4); + super->crc = ENVFS_32(crc32(0, buf + sizeof(struct envfs_super), size)); + super->sb_crc = ENVFS_32(crc32(0, buf, sizeof(struct envfs_super) - 4)); envfd = open(filename, O_WRONLY | O_CREAT); if (envfd < 0) { @@ -189,53 +189,55 @@ int envfs_load(char *filename, char *dir) goto out; } - if (super.magic != ENVFS_MAGIC) { + if ( ENVFS_32(super.magic) != ENVFS_MAGIC) { printf("envfs: wrong magic on %s\n", filename); ret = -EIO; goto out; } if (crc32(0, (unsigned char *)&super, sizeof(struct envfs_super) - 4) - != super.sb_crc) { + != ENVFS_32(super.sb_crc)) { printf("wrong crc on env superblock\n"); goto out; } - buf = xmalloc(super.size); + size = ENVFS_32(super.size); + buf = xmalloc(size); buf_free = buf; - ret = read(envfd, buf, super.size); - if (ret < super.size) { + ret = read(envfd, buf, size); + if (ret < size) { perror("read"); goto out; } - if (crc32(0, (unsigned char *)buf, super.size) - != super.crc) { + if (crc32(0, (unsigned char *)buf, size) + != ENVFS_32(super.crc)) { printf("wrong crc on env\n"); goto out; } - - size = super.size; - + while (size) { struct envfs_inode *inode; - + uint32_t inode_size,inode_namelen; + inode = (struct envfs_inode *)buf; - - if (inode->magic != ENVFS_INODE_MAGIC) { + + if (ENVFS_32(inode->magic) != ENVFS_INODE_MAGIC) { printf("envfs: wrong magic on %s\n", filename); ret = -EIO; goto out; } - + inode_size = ENVFS_32(inode->size); + inode_namelen = ENVFS_32(inode->namelen); + debug("loading %s size %d namelen %d\n", inode->data, - inode->size, inode->namelen); - + inode_size, inode_namelen); + str = concat_path_file(dir, inode->data); tmp = strdup(str); make_directory(dirname(tmp)); free(tmp); - + fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644); free(str); if (fd < 0) { @@ -244,19 +246,19 @@ int envfs_load(char *filename, char *dir) goto out; } - namelen_full = PAD4(inode->namelen); + namelen_full = PAD4(inode_namelen); ret = write(fd, buf + namelen_full + sizeof(struct envfs_inode), - inode->size); - if (ret < inode->size) { + inode_size); + if (ret < inode_size) { perror("write"); close(fd); goto out; } close(fd); - - buf += PAD4(inode->namelen) + PAD4(inode->size) + + + buf += PAD4(inode_namelen) + PAD4(inode_size) + sizeof(struct envfs_inode); - size -= PAD4(inode->namelen) + PAD4(inode->size) + + size -= PAD4(inode_namelen) + PAD4(inode_size) + sizeof(struct envfs_inode); } diff --git a/include/cramfs/cramfs_fs.h b/include/cramfs/cramfs_fs.h index 9f1b1d529c..6f2ee6871a 100644 --- a/include/cramfs/cramfs_fs.h +++ b/include/cramfs/cramfs_fs.h @@ -84,6 +84,10 @@ struct cramfs_super { | CRAMFS_FLAG_WRONG_SIGNATURE \ | CRAMFS_FLAG_SHIFTED_ROOT_OFFSET ) +#ifndef __BYTE_ORDER +#error "No byte order defined in __BYTE_ORDER" +#endif + #if __BYTE_ORDER == __LITTLE_ENDIAN #define CRAMFS_16(x) (x) #define CRAMFS_24(x) (x) diff --git a/include/envfs.h b/include/envfs.h index 6f12342834..406bc61e75 100644 --- a/include/envfs.h +++ b/include/envfs.h @@ -1,7 +1,7 @@ #ifndef _ENVFS_H #define _ENVFS_H -#define ENVFS_MAGIC 0x798fba79 /* some random number */ +#define ENVFS_MAGIC 0x798fba79 /* some random number */ #define ENVFS_INODE_MAGIC 0x67a8c78d #define ENVFS_END_MAGIC 0x6a87d6cd #define ENVFS_SIGNATURE "U-Boot envfs" @@ -30,7 +30,12 @@ struct envfs_super { uint32_t sb_crc; /* crc for the superblock */ }; +#ifndef __BYTE_ORDER +#error "No byte order defined in __BYTE_ORDER" +#endif + #if __BYTE_ORDER == __LITTLE_ENDIAN +#warning "envfs compiled on little endian host" #define ENVFS_16(x) (x) #define ENVFS_24(x) (x) #define ENVFS_32(x) (x) @@ -39,6 +44,7 @@ struct envfs_super { #define ENVFS_SET_OFFSET(x,y) ((x)->offset = (y)) #define ENVFS_SET_NAMELEN(x,y) ((x)->namelen = (y)) #elif __BYTE_ORDER == __BIG_ENDIAN +#warning "envfs compiled on big endian host" #ifdef __KERNEL__ #define ENVFS_16(x) swab16(x) #define ENVFS_24(x) ((swab32(x)) >> 8) @@ -48,6 +54,16 @@ struct envfs_super { #define ENVFS_24(x) ((bswap_32(x)) >> 8) #define ENVFS_32(x) bswap_32(x) #endif /* not __KERNEL__ */ +#define CRAMFS_GET_NAMELEN(x) (((u8*)(x))[8] & 0x3f) +#define CRAMFS_GET_OFFSET(x) ((CRAMFS_24(((u32*)(x))[2] & 0xffffff) << 2) |\ + ((((u32*)(x))[2] & 0xc0000000) >> 30)) +#define CRAMFS_SET_NAMELEN(x,y) (((u8*)(x))[8] = (((0x3f & (y))) | \ + (0xc0 & ((u8*)(x))[8]))) +#define CRAMFS_SET_OFFSET(x,y) (((u32*)(x))[2] = (((y) & 3) << 30) | \ + CRAMFS_24((((y) & 0x03ffffff) >> 2)) | \ + (((u32)(((u8*)(x))[8] & 0x3f)) << 24)) +#else +#error "__BYTE_ORDER must be __LITTLE_ENDIAN or __BIG_ENDIAN" #endif #endif /* _ENVFS_H */ |