Commit d455d8789d5b35a39a0a179b3af4b423db13bfdd

Authored by Suriyan Ramasami
Committed by Tom Rini
1 parent 96b1046d1c

fs: API changes enabling extra parameter to return size of type loff_t

The sandbox/ext4/fat/generic fs commands do not gracefully deal with files
greater than 2GB. Negative values are returned in such cases.

To handle this, the fs functions have been modified to take an additional
parameter of type "* loff_t" which is then populated. The return value
of the fs functions are used only for error conditions.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>
Acked-by: Simon Glass <sjg@chromium.org>
[trini: Update board/gdsys/p1022/controlcenterd-id.c,
drivers/fpga/zynqpl.c for changes]
Signed-off-by: Tom Rini <trini@ti.com>

Showing 11 changed files with 138 additions and 122 deletions Side-by-side Diff

board/gdsys/p1022/controlcenterd-id.c
... ... @@ -932,11 +932,12 @@
932 932 struct key_program header;
933 933 uint32_t crc;
934 934 uint8_t buf[12];
935   - int i;
  935 + loff_t i;
936 936  
937 937 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
938 938 goto failure;
939   - i = fs_read(path, (ulong)buf, 0, 12);
  939 + if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
  940 + goto failure;
940 941 if (i < 12)
941 942 goto failure;
942 943 header.magic = get_unaligned_be32(buf);
... ... @@ -951,8 +952,9 @@
951 952 goto failure;
952 953 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
953 954 goto failure;
954   - i = fs_read(path, (ulong)result, 0,
955   - sizeof(struct key_program) + header.code_size);
  955 + if (fs_read(path, (ulong)result, 0,
  956 + sizeof(struct key_program) + header.code_size, &i) < 0)
  957 + goto failure;
956 958 if (i <= 0)
957 959 goto failure;
958 960 *result = header;
... ... @@ -1043,7 +1045,7 @@
1043 1045 const char *image_path = "/ccdm.itb";
1044 1046 char *mac_path = NULL;
1045 1047 ulong image_addr;
1046   - size_t image_size;
  1048 + loff_t image_size;
1047 1049 uint32_t err;
1048 1050  
1049 1051 printf("CCDM S2\n");
1050 1052  
... ... @@ -1085,10 +1087,11 @@
1085 1087 image_addr = (ulong)get_image_location();
1086 1088 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1087 1089 goto failure;
1088   - image_size = fs_read(image_path, image_addr, 0, 0);
  1090 + if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
  1091 + goto failure;
1089 1092 if (image_size <= 0)
1090 1093 goto failure;
1091   - printf("CCDM image found on %s, %d bytes\n", mmcdev, image_size);
  1094 + printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
1092 1095  
1093 1096 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1094 1097 if (!hmac_blob) {
... ... @@ -51,6 +51,23 @@
51 51 " If 'pos' is 0 or omitted, the file is read from the start."
52 52 )
53 53  
  54 +static int do_save_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
  55 + char * const argv[])
  56 +{
  57 + return do_save(cmdtp, flag, argc, argv, FS_TYPE_ANY);
  58 +}
  59 +
  60 +U_BOOT_CMD(
  61 + save, 7, 0, do_save_wrapper,
  62 + "save file to a filesystem",
  63 + "<interface> <dev[:part]> <addr> <filename> bytes [pos]\n"
  64 + " - Save binary file 'filename' to partition 'part' on device\n"
  65 + " type 'interface' instance 'dev' from addr 'addr' in memory.\n"
  66 + " 'bytes' gives the size to save in bytes and is mandatory.\n"
  67 + " 'pos' gives the file byte position to start writing to.\n"
  68 + " If 'pos' is 0 or omitted, the file is written from the start."
  69 +)
  70 +
54 71 static int do_ls_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
55 72 char * const argv[])
56 73 {
drivers/fpga/zynqpl.c
... ... @@ -406,8 +406,8 @@
406 406 unsigned long ts; /* Timestamp */
407 407 u32 isr_status, swap;
408 408 u32 partialbit = 0;
409   - u32 blocksize;
410   - u32 pos = 0;
  409 + loff_t blocksize, actread;
  410 + loff_t pos = 0;
411 411 int fstype;
412 412 char *interface, *dev_part, *filename;
413 413  
... ... @@ -420,7 +420,7 @@
420 420 if (fs_set_blk_dev(interface, dev_part, fstype))
421 421 return FPGA_FAIL;
422 422  
423   - if (fs_read(filename, (u32) buf, pos, blocksize) < 0)
  423 + if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
424 424 return FPGA_FAIL;
425 425  
426 426 if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
427 427  
... ... @@ -443,10 +443,10 @@
443 443 return FPGA_FAIL;
444 444  
445 445 if (bsize > blocksize) {
446   - if (fs_read(filename, (u32) buf, pos, blocksize) < 0)
  446 + if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
447 447 return FPGA_FAIL;
448 448 } else {
449   - if (fs_read(filename, (u32) buf, pos, bsize) < 0)
  449 + if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
450 450 return FPGA_FAIL;
451 451 }
452 452 } while (bsize > blocksize);
... ... @@ -184,16 +184,9 @@
184 184 return ret == 0;
185 185 }
186 186  
187   -int ext4fs_size(const char *filename)
  187 +int ext4fs_size(const char *filename, loff_t *size)
188 188 {
189   - loff_t size;
190   - int ret;
191   -
192   - ret = ext4fs_open(filename, &size);
193   - if (ret)
194   - return ret;
195   - else
196   - return size;
  189 + return ext4fs_open(filename, size);
197 190 }
198 191  
199 192 int ext4fs_read(char *buf, loff_t len, loff_t *actread)
200 193  
... ... @@ -217,10 +210,10 @@
217 210 return 0;
218 211 }
219 212  
220   -int ext4_read_file(const char *filename, void *buf, int offset, int len)
  213 +int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  214 + loff_t *len_read)
221 215 {
222 216 loff_t file_len;
223   - loff_t len_read;
224 217 int ret;
225 218  
226 219 if (offset != 0) {
... ... @@ -237,12 +230,7 @@
237 230 if (len == 0)
238 231 len = file_len;
239 232  
240   - ret = ext4fs_read(buf, len, &len_read);
241   -
242   - if (ret)
243   - return ret;
244   - else
245   - return len_read;
  233 + return ext4fs_read(buf, len, len_read);
246 234 }
247 235  
248 236 int ext4fs_uuid(char *uuid_str)
... ... @@ -1248,16 +1248,9 @@
1248 1248 return ret == 0;
1249 1249 }
1250 1250  
1251   -int fat_size(const char *filename)
  1251 +int fat_size(const char *filename, loff_t *size)
1252 1252 {
1253   - loff_t size;
1254   - int ret;
1255   -
1256   - ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1257   - if (ret)
1258   - return ret;
1259   - else
1260   - return size;
  1253 + return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
1261 1254 }
1262 1255  
1263 1256 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1264 1257  
1265 1258  
1266 1259  
1267 1260  
... ... @@ -1280,18 +1273,16 @@
1280 1273 return actread;
1281 1274 }
1282 1275  
1283   -int fat_read_file(const char *filename, void *buf, int offset, int len)
  1276 +int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  1277 + loff_t *actread)
1284 1278 {
1285 1279 int ret;
1286   - loff_t actread;
1287 1280  
1288   - ret = file_fat_read_at(filename, offset, buf, len, &actread);
1289   - if (ret) {
  1281 + ret = file_fat_read_at(filename, offset, buf, len, actread);
  1282 + if (ret)
1290 1283 printf("** Unable to read file %s **\n", filename);
1291   - return ret;
1292   - }
1293 1284  
1294   - return actread;
  1285 + return ret;
1295 1286 }
1296 1287  
1297 1288 void fat_close(void)
... ... @@ -47,19 +47,21 @@
47 47 return 0;
48 48 }
49 49  
50   -static inline int fs_size_unsupported(const char *filename)
  50 +static inline int fs_size_unsupported(const char *filename, loff_t *size)
51 51 {
52 52 return -1;
53 53 }
54 54  
55 55 static inline int fs_read_unsupported(const char *filename, void *buf,
56   - int offset, int len)
  56 + loff_t offset, loff_t len,
  57 + loff_t *actread)
57 58 {
58 59 return -1;
59 60 }
60 61  
61 62 static inline int fs_write_unsupported(const char *filename, void *buf,
62   - int offset, int len)
  63 + loff_t offset, loff_t len,
  64 + loff_t *actwrite)
63 65 {
64 66 return -1;
65 67 }
... ... @@ -88,9 +90,11 @@
88 90 disk_partition_t *fs_partition);
89 91 int (*ls)(const char *dirname);
90 92 int (*exists)(const char *filename);
91   - int (*size)(const char *filename);
92   - int (*read)(const char *filename, void *buf, int offset, int len);
93   - int (*write)(const char *filename, void *buf, int offset, int len);
  93 + int (*size)(const char *filename, loff_t *size);
  94 + int (*read)(const char *filename, void *buf, loff_t offset,
  95 + loff_t len, loff_t *actread);
  96 + int (*write)(const char *filename, void *buf, loff_t offset,
  97 + loff_t len, loff_t *actwrite);
94 98 void (*close)(void);
95 99 int (*uuid)(char *uuid_str);
96 100 };
97 101  
... ... @@ -106,7 +110,11 @@
106 110 .exists = fat_exists,
107 111 .size = fat_size,
108 112 .read = fat_read_file,
  113 +#ifdef CONFIG_FAT_WRITE
  114 + .write = file_fat_write,
  115 +#else
109 116 .write = fs_write_unsupported,
  117 +#endif
110 118 .uuid = fs_uuid_unsupported,
111 119 },
112 120 #endif
113 121  
... ... @@ -120,7 +128,11 @@
120 128 .exists = ext4fs_exists,
121 129 .size = ext4fs_size,
122 130 .read = ext4_read_file,
  131 +#ifdef CONFIG_CMD_EXT4_WRITE
  132 + .write = ext4_write_file,
  133 +#else
123 134 .write = fs_write_unsupported,
  135 +#endif
124 136 .uuid = ext4fs_uuid,
125 137 },
126 138 #endif
127 139  
128 140  
... ... @@ -251,20 +263,21 @@
251 263 return ret;
252 264 }
253 265  
254   -int fs_size(const char *filename)
  266 +int fs_size(const char *filename, loff_t *size)
255 267 {
256 268 int ret;
257 269  
258 270 struct fstype_info *info = fs_get_info(fs_type);
259 271  
260   - ret = info->size(filename);
  272 + ret = info->size(filename, size);
261 273  
262 274 fs_close();
263 275  
264 276 return ret;
265 277 }
266 278  
267   -int fs_read(const char *filename, ulong addr, int offset, int len)
  279 +int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  280 + loff_t *actread)
268 281 {
269 282 struct fstype_info *info = fs_get_info(fs_type);
270 283 void *buf;
271 284  
... ... @@ -275,11 +288,11 @@
275 288 * means read the whole file.
276 289 */
277 290 buf = map_sysmem(addr, len);
278   - ret = info->read(filename, buf, offset, len);
  291 + ret = info->read(filename, buf, offset, len, actread);
279 292 unmap_sysmem(buf);
280 293  
281 294 /* If we requested a specific number of bytes, check we got it */
282   - if (ret >= 0 && len && ret != len) {
  295 + if (ret == 0 && len && *actread != len) {
283 296 printf("** Unable to read file %s **\n", filename);
284 297 ret = -1;
285 298 }
286 299  
287 300  
... ... @@ -288,17 +301,18 @@
288 301 return ret;
289 302 }
290 303  
291   -int fs_write(const char *filename, ulong addr, int offset, int len)
  304 +int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  305 + loff_t *actwrite)
292 306 {
293 307 struct fstype_info *info = fs_get_info(fs_type);
294 308 void *buf;
295 309 int ret;
296 310  
297 311 buf = map_sysmem(addr, len);
298   - ret = info->write(filename, buf, offset, len);
  312 + ret = info->write(filename, buf, offset, len, actwrite);
299 313 unmap_sysmem(buf);
300 314  
301   - if (ret >= 0 && ret != len) {
  315 + if (ret < 0 && len != *actwrite) {
302 316 printf("** Unable to write file %s **\n", filename);
303 317 ret = -1;
304 318 }
... ... @@ -310,7 +324,7 @@
310 324 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
311 325 int fstype)
312 326 {
313   - int size;
  327 + loff_t size;
314 328  
315 329 if (argc != 4)
316 330 return CMD_RET_USAGE;
... ... @@ -318,8 +332,7 @@
318 332 if (fs_set_blk_dev(argv[1], argv[2], fstype))
319 333 return 1;
320 334  
321   - size = fs_size(argv[3]);
322   - if (size < 0)
  335 + if (fs_size(argv[3], &size) < 0)
323 336 return CMD_RET_FAILURE;
324 337  
325 338 setenv_hex("filesize", size);
... ... @@ -333,9 +346,10 @@
333 346 unsigned long addr;
334 347 const char *addr_str;
335 348 const char *filename;
336   - unsigned long bytes;
337   - unsigned long pos;
338   - int len_read;
  349 + loff_t bytes;
  350 + loff_t pos;
  351 + loff_t len_read;
  352 + int ret;
339 353 unsigned long time;
340 354 char *ep;
341 355  
342 356  
343 357  
... ... @@ -377,12 +391,12 @@
377 391 pos = 0;
378 392  
379 393 time = get_timer(0);
380   - len_read = fs_read(filename, addr, pos, bytes);
  394 + ret = fs_read(filename, addr, pos, bytes, &len_read);
381 395 time = get_timer(time);
382   - if (len_read <= 0)
  396 + if (ret < 0)
383 397 return 1;
384 398  
385   - printf("%d bytes read in %lu ms", len_read, time);
  399 + printf("%llu bytes read in %lu ms", len_read, time);
386 400 if (time > 0) {
387 401 puts(" (");
388 402 print_size(len_read / time * 1000, "/s");
... ... @@ -426,9 +440,10 @@
426 440 {
427 441 unsigned long addr;
428 442 const char *filename;
429   - unsigned long bytes;
430   - unsigned long pos;
431   - int len;
  443 + loff_t bytes;
  444 + loff_t pos;
  445 + loff_t len;
  446 + int ret;
432 447 unsigned long time;
433 448  
434 449 if (argc < 6 || argc > 7)
... ... @@ -437,8 +452,8 @@
437 452 if (fs_set_blk_dev(argv[1], argv[2], fstype))
438 453 return 1;
439 454  
440   - filename = argv[3];
441   - addr = simple_strtoul(argv[4], NULL, 16);
  455 + addr = simple_strtoul(argv[3], NULL, 16);
  456 + filename = argv[4];
442 457 bytes = simple_strtoul(argv[5], NULL, 16);
443 458 if (argc >= 7)
444 459 pos = simple_strtoul(argv[6], NULL, 16);
445 460  
446 461  
... ... @@ -446,12 +461,12 @@
446 461 pos = 0;
447 462  
448 463 time = get_timer(0);
449   - len = fs_write(filename, addr, pos, bytes);
  464 + ret = fs_write(filename, addr, pos, bytes, &len);
450 465 time = get_timer(time);
451   - if (len <= 0)
  466 + if (ret < 0)
452 467 return 1;
453 468  
454   - printf("%d bytes written in %lu ms", len, time);
  469 + printf("%llu bytes written in %lu ms", len, time);
455 470 if (time > 0) {
456 471 puts(" (");
457 472 print_size(len / time * 1000, "/s");
fs/sandbox/sandboxfs.c
... ... @@ -103,47 +103,36 @@
103 103 return ret == 0;
104 104 }
105 105  
106   -int sandbox_fs_size(const char *filename)
  106 +int sandbox_fs_size(const char *filename, loff_t *size)
107 107 {
108   - loff_t size;
109   - int ret;
110   -
111   - ret = os_get_filesize(filename, &size);
112   - if (ret)
113   - return ret;
114   - else
115   - return size;
  108 + return os_get_filesize(filename, size);
116 109 }
117 110  
118 111 void sandbox_fs_close(void)
119 112 {
120 113 }
121 114  
122   -int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
  115 +int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
  116 + loff_t *actread)
123 117 {
124 118 int ret;
125   - loff_t actread;
126 119  
127   - ret = sandbox_fs_read_at(filename, offset, buf, len, &actread);
128   - if (ret) {
  120 + ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
  121 + if (ret)
129 122 printf("** Unable to read file %s **\n", filename);
130   - return ret;
131   - }
132 123  
133   - return actread;
  124 + return ret;
134 125 }
135 126  
136   -int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
  127 +int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
  128 + loff_t len, loff_t *actwrite)
137 129 {
138 130 int ret;
139   - loff_t actwrite;
140 131  
141   - ret = sandbox_fs_write_at(filename, offset, buf, len, &actwrite);
142   - if (ret) {
  132 + ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
  133 + if (ret)
143 134 printf("** Unable to write file %s **\n", filename);
144   - return ret;
145   - }
146 135  
147   - return actwrite;
  136 + return ret;
148 137 }
... ... @@ -138,14 +138,15 @@
138 138 void ext4fs_reinit_global(void);
139 139 int ext4fs_ls(const char *dirname);
140 140 int ext4fs_exists(const char *filename);
141   -int ext4fs_size(const char *filename);
  141 +int ext4fs_size(const char *filename, loff_t *size);
142 142 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
143 143 int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
144 144 void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
145 145 long int read_allocated_block(struct ext2_inode *inode, int fileblock);
146 146 int ext4fs_probe(block_dev_desc_t *fs_dev_desc,
147 147 disk_partition_t *fs_partition);
148   -int ext4_read_file(const char *filename, void *buf, int offset, int len);
  148 +int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  149 + loff_t *actread);
149 150 int ext4_read_superblock(char *buffer);
150 151 int ext4fs_uuid(char *uuid_str);
151 152 #endif
... ... @@ -198,7 +198,7 @@
198 198 int file_fat_detectfs(void);
199 199 int file_fat_ls(const char *dir);
200 200 int fat_exists(const char *filename);
201   -int fat_size(const char *filename);
  201 +int fat_size(const char *filename, loff_t *size);
202 202 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
203 203 loff_t maxsize, loff_t *actread);
204 204 int file_fat_read(const char *filename, void *buffer, int maxsize);
... ... @@ -208,7 +208,8 @@
208 208  
209 209 int file_fat_write(const char *filename, void *buf, loff_t offset, loff_t len,
210 210 loff_t *actwrite);
211   -int fat_read_file(const char *filename, void *buf, int offset, int len);
  211 +int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  212 + loff_t *actread);
212 213 void fat_close(void);
213 214 #endif /* _FAT_H_ */
... ... @@ -51,32 +51,41 @@
51 51 int fs_exists(const char *filename);
52 52  
53 53 /*
54   - * Determine a file's size
  54 + * fs_size - Determine a file's size
55 55 *
56   - * Returns the file's size in bytes, or a negative value if it doesn't exist.
  56 + * @filename: Name of the file
  57 + * @size: Size of file
  58 + * @return 0 if ok with valid *size, negative on error
57 59 */
58   -int fs_size(const char *filename);
  60 +int fs_size(const char *filename, loff_t *size);
59 61  
60 62 /*
61   - * Read file "filename" from the partition previously set by fs_set_blk_dev(),
62   - * to address "addr", starting at byte offset "offset", and reading "len"
63   - * bytes. "offset" may be 0 to read from the start of the file. "len" may be
64   - * 0 to read the entire file. Note that not all filesystem types support
65   - * either/both offset!=0 or len!=0.
  63 + * fs_read - Read file from the partition previously set by fs_set_blk_dev()
  64 + * Note that not all filesystem types support either/both offset!=0 or len!=0.
66 65 *
67   - * Returns number of bytes read on success. Returns <= 0 on error.
  66 + * @filename: Name of file to read from
  67 + * @addr: The address to read into
  68 + * @offset: The offset in file to read from
  69 + * @len: The number of bytes to read. Maybe 0 to read entire file
  70 + * @actread: Returns the actual number of bytes read
  71 + * @return 0 if ok with valid *actread, -1 on error conditions
68 72 */
69   -int fs_read(const char *filename, ulong addr, int offset, int len);
  73 +int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  74 + loff_t *actread);
70 75  
71 76 /*
72   - * Write file "filename" to the partition previously set by fs_set_blk_dev(),
73   - * from address "addr", starting at byte offset "offset", and writing "len"
74   - * bytes. "offset" may be 0 to write to the start of the file. Note that not
75   - * all filesystem types support offset!=0.
  77 + * fs_write - Write file to the partition previously set by fs_set_blk_dev()
  78 + * Note that not all filesystem types support offset!=0.
76 79 *
77   - * Returns number of bytes read on success. Returns <= 0 on error.
  80 + * @filename: Name of file to read from
  81 + * @addr: The address to read into
  82 + * @offset: The offset in file to read from. Maybe 0 to write to start of file
  83 + * @len: The number of bytes to write
  84 + * @actwrite: Returns the actual number of bytes written
  85 + * @return 0 if ok with valid *actwrite, -1 on error conditions
78 86 */
79   -int fs_write(const char *filename, ulong addr, int offset, int len);
  87 +int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  88 + loff_t *actwrite);
80 89  
81 90 /*
82 91 * Common implementation for various filesystem commands, optionally limited
... ... @@ -28,9 +28,11 @@
28 28 void sandbox_fs_close(void);
29 29 int sandbox_fs_ls(const char *dirname);
30 30 int sandbox_fs_exists(const char *filename);
31   -int sandbox_fs_size(const char *filename);
32   -int fs_read_sandbox(const char *filename, void *buf, int offset, int len);
33   -int fs_write_sandbox(const char *filename, void *buf, int offset, int len);
  31 +int sandbox_fs_size(const char *filename, loff_t *size);
  32 +int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
  33 + loff_t *actread);
  34 +int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
  35 + loff_t len, loff_t *actwrite);
34 36  
35 37 #endif