mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 08:42:40 +01:00 
			
		
		
		
	Added conversion to/from little-endian on disk
Required to support big-endian processors, with the most notable being the PowerPC architecture. On little-endian architectures, these conversions can be optimized out and have no code impact. Initial patch provided by gmouchard
This commit is contained in:
		
							
								
								
									
										139
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										139
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -21,37 +21,7 @@ | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <assert.h> | ||||
| #include <sys/param.h> | ||||
|  | ||||
| #if (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 3) | ||||
| #define LFS_BSWAP32(v) __builtin_bswap32(v); | ||||
| #else | ||||
| #define LFS_BSWAP32(v) ((((uint32_t)(v) & 0xff000000) >> 24) | (((uint32_t)(v) & 0x00ff0000) >> 8) | (((uint32_t)(v) & 0x0000ff00) << 8) | (((uint32_t)(v) & 0x000000ff) << 24)) | ||||
| #endif | ||||
|  | ||||
| #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||
| #define LFS_ENDIAN_CONV_U32(v) do { (v) = LFS_BSWAP32(v); } while(0) | ||||
| #else | ||||
| #define LFS_ENDIAN_CONV_U32(v) | ||||
| #endif | ||||
|  | ||||
| #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||
| #define LFS_ENDIAN_CONV_DISK_DIR(v) do { LFS_ENDIAN_CONV_U32(v.rev); LFS_ENDIAN_CONV_U32(v.size); LFS_ENDIAN_CONV_U32(v.tail[0]); LFS_ENDIAN_CONV_U32(v.tail[1]); } while(0)  | ||||
| #else | ||||
| #define LFS_ENDIAN_CONV_DISK_DIR(v) | ||||
| #endif | ||||
|  | ||||
| #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||
| #define LFS_ENDIAN_CONV_DISK_ENTRY(v) do { LFS_ENDIAN_CONV_U32(v.u.dir[0]); LFS_ENDIAN_CONV_U32(v.u.dir[1]); } while(0) | ||||
| #else | ||||
| #define LFS_ENDIAN_CONV_DISK_ENTRY(v) | ||||
| #endif | ||||
|  | ||||
| #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||
| #define LFS_ENDIAN_CONV_DISK_SUPERBLOCK(v) do { LFS_ENDIAN_CONV_U32(v.root[0]); LFS_ENDIAN_CONV_U32(v.root[1]); LFS_ENDIAN_CONV_U32(v.block_size); LFS_ENDIAN_CONV_U32(v.block_count); LFS_ENDIAN_CONV_U32(v.version); } while(0) | ||||
| #else | ||||
| #define LFS_ENDIAN_CONV_DISK_SUPERBLOCK(v) | ||||
| #endif | ||||
|  | ||||
| /// Caching block device operations /// | ||||
| static int lfs_cache_read(lfs_t *lfs, lfs_cache_t *rcache, | ||||
| @@ -356,6 +326,48 @@ static void lfs_alloc_ack(lfs_t *lfs) { | ||||
| } | ||||
|  | ||||
|  | ||||
| /// Endian swapping functions /// | ||||
| static void lfs_dir_fromle32(struct lfs_disk_dir *d) { | ||||
|     d->rev     = lfs_fromle32(d->rev); | ||||
|     d->size    = lfs_fromle32(d->size); | ||||
|     d->tail[0] = lfs_fromle32(d->tail[0]); | ||||
|     d->tail[1] = lfs_fromle32(d->tail[1]); | ||||
| } | ||||
|  | ||||
| static void lfs_dir_tole32(struct lfs_disk_dir *d) { | ||||
|     d->rev     = lfs_tole32(d->rev); | ||||
|     d->size    = lfs_tole32(d->size); | ||||
|     d->tail[0] = lfs_tole32(d->tail[0]); | ||||
|     d->tail[1] = lfs_tole32(d->tail[1]); | ||||
| } | ||||
|  | ||||
| static void lfs_entry_fromle32(struct lfs_disk_entry *d) { | ||||
|     d->u.dir[0] = lfs_fromle32(d->u.dir[0]); | ||||
|     d->u.dir[1] = lfs_fromle32(d->u.dir[1]); | ||||
| } | ||||
|  | ||||
| static void lfs_entry_tole32(struct lfs_disk_entry *d) { | ||||
|     d->u.dir[0] = lfs_tole32(d->u.dir[0]); | ||||
|     d->u.dir[1] = lfs_tole32(d->u.dir[1]); | ||||
| } | ||||
|  | ||||
| static void lfs_superblock_fromle32(struct lfs_disk_superblock *d) { | ||||
|     d->root[0]     = lfs_fromle32(d->root[0]); | ||||
|     d->root[1]     = lfs_fromle32(d->root[1]); | ||||
|     d->block_size  = lfs_fromle32(d->block_size); | ||||
|     d->block_count = lfs_fromle32(d->block_count); | ||||
|     d->version     = lfs_fromle32(d->version); | ||||
| } | ||||
|  | ||||
| static void lfs_superblock_tole32(struct lfs_disk_superblock *d) { | ||||
|     d->root[0]     = lfs_tole32(d->root[0]); | ||||
|     d->root[1]     = lfs_tole32(d->root[1]); | ||||
|     d->block_size  = lfs_tole32(d->block_size); | ||||
|     d->block_count = lfs_tole32(d->block_count); | ||||
|     d->version     = lfs_tole32(d->version); | ||||
| } | ||||
|  | ||||
|  | ||||
| /// Metadata pair and directory operations /// | ||||
| static inline void lfs_pairswap(lfs_block_t pair[2]) { | ||||
|     lfs_block_t t = pair[0]; | ||||
| @@ -397,10 +409,10 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_dir_t *dir) { | ||||
|     // rather than clobbering one of the blocks we just pretend | ||||
|     // the revision may be valid | ||||
|     int err = lfs_bd_read(lfs, dir->pair[0], 0, &dir->d.rev, 4); | ||||
|     dir->d.rev = lfs_fromle32(dir->d.rev); | ||||
|     if (err) { | ||||
|         return err; | ||||
|     } | ||||
|     LFS_ENDIAN_CONV_U32(dir->d.rev); // little-endian to host conversion | ||||
|  | ||||
|     // set defaults | ||||
|     dir->d.rev += 1; | ||||
| @@ -423,10 +435,10 @@ static int lfs_dir_fetch(lfs_t *lfs, | ||||
|     for (int i = 0; i < 2; i++) { | ||||
|         struct lfs_disk_dir test; | ||||
|         int err = lfs_bd_read(lfs, tpair[i], 0, &test, sizeof(test)); | ||||
|         lfs_dir_fromle32(&test); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
|         LFS_ENDIAN_CONV_DISK_DIR(test); // little-endian to host conversion | ||||
|  | ||||
|         if (valid && lfs_scmp(test.rev, dir->d.rev) < 0) { | ||||
|             continue; | ||||
| @@ -437,10 +449,10 @@ static int lfs_dir_fetch(lfs_t *lfs, | ||||
|             continue; | ||||
|         } | ||||
|  | ||||
|         LFS_ENDIAN_CONV_DISK_DIR(test); // host to little-endian conversion | ||||
|         uint32_t crc = 0xffffffff; | ||||
|         lfs_dir_tole32(&test); | ||||
|         lfs_crc(&crc, &test, sizeof(test)); | ||||
|         LFS_ENDIAN_CONV_DISK_DIR(test); // little-endian to host conversion | ||||
|         lfs_dir_fromle32(&test); | ||||
|         err = lfs_bd_crc(lfs, tpair[i], sizeof(test), | ||||
|                 (0x7fffffff & test.size) - sizeof(test), &crc); | ||||
|         if (err) { | ||||
| @@ -500,9 +512,10 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir, | ||||
|             } | ||||
|  | ||||
|             uint32_t crc = 0xffffffff; | ||||
|             lfs_dir_t _dir; _dir.d = dir->d; LFS_ENDIAN_CONV_DISK_DIR(_dir.d); // host to little-endian conversion | ||||
|             lfs_crc(&crc, &_dir.d, sizeof(_dir.d)); | ||||
|             err = lfs_bd_prog(lfs, dir->pair[0], 0, &_dir.d, sizeof(_dir.d)); | ||||
|             lfs_dir_tole32(&dir->d); | ||||
|             lfs_crc(&crc, &dir->d, sizeof(dir->d)); | ||||
|             err = lfs_bd_prog(lfs, dir->pair[0], 0, &dir->d, sizeof(dir->d)); | ||||
|             lfs_dir_fromle32(&dir->d); | ||||
|             if (err) { | ||||
|                 if (err == LFS_ERR_CORRUPT) { | ||||
|                     goto relocate; | ||||
| @@ -549,9 +562,9 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir, | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             LFS_ENDIAN_CONV_U32(crc); // host to little-endian conversion | ||||
|             crc = lfs_tole32(crc); | ||||
|             err = lfs_bd_prog(lfs, dir->pair[0], newoff, &crc, 4); | ||||
|             LFS_ENDIAN_CONV_U32(crc); // little-endian to host conversion | ||||
|             crc = lfs_fromle32(crc); | ||||
|             if (err) { | ||||
|                 if (err == LFS_ERR_CORRUPT) { | ||||
|                     goto relocate; | ||||
| @@ -624,12 +637,14 @@ relocate: | ||||
| } | ||||
|  | ||||
| static int lfs_dir_update(lfs_t *lfs, lfs_dir_t *dir, | ||||
|         const lfs_entry_t *entry, const void *data) { | ||||
|     lfs_entry_t _entry; _entry.d = entry->d; LFS_ENDIAN_CONV_DISK_ENTRY(_entry.d); // host to little-endian conversion | ||||
|     return lfs_dir_commit(lfs, dir, (struct lfs_region[]){ | ||||
|             {entry->off, sizeof(entry->d), &_entry.d, sizeof(entry->d)}, | ||||
|         lfs_entry_t *entry, const void *data) { | ||||
|     lfs_entry_tole32(&entry->d); | ||||
|     int err = lfs_dir_commit(lfs, dir, (struct lfs_region[]){ | ||||
|             {entry->off, sizeof(entry->d), &entry->d, sizeof(entry->d)}, | ||||
|             {entry->off+sizeof(entry->d), entry->d.nlen, data, entry->d.nlen} | ||||
|         }, data ? 2 : 1); | ||||
|     lfs_entry_fromle32(&entry->d); | ||||
|     return err; | ||||
| } | ||||
|  | ||||
| static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir, | ||||
| @@ -638,11 +653,14 @@ static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir, | ||||
|     while (true) { | ||||
|         if (dir->d.size + lfs_entry_size(entry) <= lfs->cfg->block_size) { | ||||
|             entry->off = dir->d.size - 4; | ||||
|             lfs_entry_t _entry; _entry.d = entry->d; LFS_ENDIAN_CONV_DISK_ENTRY(_entry.d); // host to little-endian conversion | ||||
|             return lfs_dir_commit(lfs, dir, (struct lfs_region[]){ | ||||
|                     {entry->off, 0, &_entry.d, sizeof(entry->d)}, | ||||
|  | ||||
|             lfs_entry_tole32(&entry->d); | ||||
|             int err = lfs_dir_commit(lfs, dir, (struct lfs_region[]){ | ||||
|                     {entry->off, 0, &entry->d, sizeof(entry->d)}, | ||||
|                     {entry->off, 0, data, entry->d.nlen} | ||||
|                 }, 2); | ||||
|             lfs_entry_fromle32(&entry->d); | ||||
|             return err; | ||||
|         } | ||||
|  | ||||
|         // we need to allocate a new dir block | ||||
| @@ -656,11 +674,12 @@ static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir, | ||||
|             newdir.d.tail[0] = dir->d.tail[0]; | ||||
|             newdir.d.tail[1] = dir->d.tail[1]; | ||||
|             entry->off = newdir.d.size - 4; | ||||
|             lfs_entry_t _entry; _entry.d = entry->d; LFS_ENDIAN_CONV_DISK_ENTRY(_entry.d); // host to little-endian conversion | ||||
|             lfs_entry_tole32(&entry->d); | ||||
|             err = lfs_dir_commit(lfs, &newdir, (struct lfs_region[]){ | ||||
|                     {entry->off, 0, &_entry.d, sizeof(entry->d)}, | ||||
|                     {entry->off, 0, &entry->d, sizeof(entry->d)}, | ||||
|                     {entry->off, 0, data, entry->d.nlen} | ||||
|                 }, 2); | ||||
|             lfs_entry_fromle32(&entry->d); | ||||
|             if (err) { | ||||
|                 return err; | ||||
|             } | ||||
| @@ -746,10 +765,10 @@ static int lfs_dir_next(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) { | ||||
|  | ||||
|     int err = lfs_bd_read(lfs, dir->pair[0], dir->off, | ||||
|             &entry->d, sizeof(entry->d)); | ||||
|     lfs_entry_fromle32(&entry->d); | ||||
|     if (err) { | ||||
|         return err; | ||||
|     } | ||||
|     LFS_ENDIAN_CONV_DISK_ENTRY(entry->d); // little-endian to host conversion | ||||
|  | ||||
|     entry->off = dir->off; | ||||
|     dir->off += lfs_entry_size(entry); | ||||
| @@ -1108,10 +1127,10 @@ static int lfs_ctz_find(lfs_t *lfs, | ||||
|                 lfs_ctz(current)); | ||||
|  | ||||
|         int err = lfs_cache_read(lfs, rcache, pcache, head, 4*skip, &head, 4); | ||||
|         head = lfs_fromle32(head); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
|         LFS_ENDIAN_CONV_U32(head); // little-endian to host conversion | ||||
|  | ||||
|         assert(head >= 2 && head <= lfs->cfg->block_count); | ||||
|         current -= 1 << skip; | ||||
| @@ -1184,10 +1203,10 @@ static int lfs_ctz_extend(lfs_t *lfs, | ||||
|             lfs_size_t skips = lfs_ctz(index) + 1; | ||||
|  | ||||
|             for (lfs_off_t i = 0; i < skips; i++) { | ||||
|                 LFS_ENDIAN_CONV_U32(head); // host to little-endian conversion | ||||
|                 head = lfs_tole32(head); | ||||
|                 int err = lfs_cache_prog(lfs, pcache, rcache, | ||||
|                         nblock, 4*i, &head, 4); | ||||
|                 LFS_ENDIAN_CONV_U32(head); // little-endian to host conversion | ||||
|                 head = lfs_fromle32(head); | ||||
|                 if (err) { | ||||
|                     if (err == LFS_ERR_CORRUPT) { | ||||
|                         goto relocate; | ||||
| @@ -1198,10 +1217,10 @@ static int lfs_ctz_extend(lfs_t *lfs, | ||||
|                 if (i != skips-1) { | ||||
|                     err = lfs_cache_read(lfs, rcache, NULL, | ||||
|                             head, 4*i, &head, 4); | ||||
|                     head = lfs_fromle32(head); | ||||
|                     if (err) { | ||||
|                         return err; | ||||
|                     } | ||||
|                     LFS_ENDIAN_CONV_U32(head); // little-endian to host conversion | ||||
|                 } | ||||
|  | ||||
|                 assert(head >= 2 && head <= lfs->cfg->block_count); | ||||
| @@ -1243,11 +1262,11 @@ static int lfs_ctz_traverse(lfs_t *lfs, | ||||
|         lfs_block_t heads[2]; | ||||
|         int count = 2 - (index & 1); | ||||
|         err = lfs_cache_read(lfs, rcache, pcache, head, 0, &heads, count*4); | ||||
|         heads[0] = lfs_fromle32(heads[0]); | ||||
|         heads[1] = lfs_fromle32(heads[1]); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
|         LFS_ENDIAN_CONV_U32(heads[0]); // host to little-endian conversion | ||||
|         LFS_ENDIAN_CONV_U32(heads[1]); // host to little-endian conversion | ||||
|  | ||||
|         for (int i = 0; i < count-1; i++) { | ||||
|             err = cb(data, heads[i]); | ||||
| @@ -1505,10 +1524,10 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { | ||||
|         lfs_entry_t entry = {.off = file->poff}; | ||||
|         err = lfs_bd_read(lfs, cwd.pair[0], entry.off, | ||||
|                 &entry.d, sizeof(entry.d)); | ||||
|         lfs_entry_fromle32(&entry.d); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
|         LFS_ENDIAN_CONV_DISK_ENTRY(entry.d); // little-endian to host conversion | ||||
|  | ||||
|         if (entry.d.type != LFS_TYPE_REG) { | ||||
|             // sanity check valid entry | ||||
| @@ -2126,12 +2145,12 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { | ||||
|     superdir.d.size = sizeof(superdir.d) + sizeof(superblock.d) + 4; | ||||
|  | ||||
|     // write both pairs to be safe | ||||
|     lfs_superblock_tole32(&superblock.d); | ||||
|     bool valid = false; | ||||
|     for (int i = 0; i < 2; i++) { | ||||
|         lfs_superblock_t _superblock; _superblock.d = superblock.d; LFS_ENDIAN_CONV_DISK_SUPERBLOCK(_superblock.d); // host to little-endian conversion | ||||
|         int err = lfs_dir_commit(lfs, &superdir, (struct lfs_region[]){ | ||||
|                 {sizeof(superdir.d), sizeof(superblock.d), | ||||
|                  &_superblock.d, sizeof(superblock.d)} | ||||
|                  &superblock.d, sizeof(superblock.d)} | ||||
|             }, 1); | ||||
|         if (err && err != LFS_ERR_CORRUPT) { | ||||
|             return err; | ||||
| @@ -2176,10 +2195,10 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { | ||||
|     if (!err) { | ||||
|         int err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d), | ||||
|                 &superblock.d, sizeof(superblock.d)); | ||||
|         lfs_superblock_fromle32(&superblock.d); | ||||
|         if (err) { | ||||
|             return err; | ||||
|         } | ||||
|         LFS_ENDIAN_CONV_DISK_SUPERBLOCK(superblock.d); // little-endian to host conversion | ||||
|  | ||||
|         lfs->root[0] = superblock.d.root[0]; | ||||
|         lfs->root[1] = superblock.d.root[1]; | ||||
| @@ -2233,10 +2252,10 @@ int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data) { | ||||
|         while (dir.off + sizeof(entry.d) <= (0x7fffffff & dir.d.size)-4) { | ||||
|             int err = lfs_bd_read(lfs, dir.pair[0], dir.off, | ||||
|                     &entry.d, sizeof(entry.d)); | ||||
|             lfs_entry_fromle32(&entry.d); | ||||
|             if (err) { | ||||
|                 return err; | ||||
|             } | ||||
|             LFS_ENDIAN_CONV_DISK_ENTRY(entry.d); // little-endian to host conversion | ||||
|  | ||||
|             dir.off += lfs_entry_size(&entry); | ||||
|             if ((0x70 & entry.d.type) == (0x70 & LFS_TYPE_REG)) { | ||||
|   | ||||
							
								
								
									
										17
									
								
								lfs_util.h
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								lfs_util.h
									
									
									
									
									
								
							| @@ -49,6 +49,23 @@ static inline int lfs_scmp(uint32_t a, uint32_t b) { | ||||
|     return (int)(unsigned)(a - b); | ||||
| } | ||||
|  | ||||
| static inline uint32_t lfs_fromle32(uint32_t a) { | ||||
| #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | ||||
|     return a; | ||||
| #elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||
|     return __builtin_bswap32(a); | ||||
| #else | ||||
|     return (((uint8_t*)&a)[0] <<  0) | | ||||
|            (((uint8_t*)&a)[1] <<  8) | | ||||
|            (((uint8_t*)&a)[2] << 16) | | ||||
|            (((uint8_t*)&a)[3] << 24); | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static inline uint32_t lfs_tole32(uint32_t a) { | ||||
|     return lfs_fromle32(a); | ||||
| } | ||||
|  | ||||
| // CRC-32 with polynomial = 0x04c11db7 | ||||
| void lfs_crc(uint32_t *crc, const void *buffer, size_t size); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user