fs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <config.h>
  17. #include <errno.h>
  18. #include <common.h>
  19. #include <part.h>
  20. #include <ext4fs.h>
  21. #include <fat.h>
  22. #include <fs.h>
  23. #include <sandboxfs.h>
  24. #include <asm/io.h>
  25. #include <div64.h>
  26. #include <linux/math64.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. static block_dev_desc_t *fs_dev_desc;
  29. static disk_partition_t fs_partition;
  30. static int fs_type = FS_TYPE_ANY;
  31. static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
  32. disk_partition_t *fs_partition)
  33. {
  34. printf("** Unrecognized filesystem type **\n");
  35. return -1;
  36. }
  37. static inline int fs_ls_unsupported(const char *dirname)
  38. {
  39. return -1;
  40. }
  41. static inline int fs_exists_unsupported(const char *filename)
  42. {
  43. return 0;
  44. }
  45. static inline int fs_size_unsupported(const char *filename, loff_t *size)
  46. {
  47. return -1;
  48. }
  49. static inline int fs_read_unsupported(const char *filename, void *buf,
  50. loff_t offset, loff_t len,
  51. loff_t *actread)
  52. {
  53. return -1;
  54. }
  55. static inline int fs_write_unsupported(const char *filename, void *buf,
  56. loff_t offset, loff_t len,
  57. loff_t *actwrite)
  58. {
  59. return -1;
  60. }
  61. static inline void fs_close_unsupported(void)
  62. {
  63. }
  64. static inline int fs_uuid_unsupported(char *uuid_str)
  65. {
  66. return -1;
  67. }
  68. struct fstype_info {
  69. int fstype;
  70. /*
  71. * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
  72. * should be false in most cases. For "virtual" filesystems which
  73. * aren't based on a U-Boot block device (e.g. sandbox), this can be
  74. * set to true. This should also be true for the dumm entry at the end
  75. * of fstypes[], since that is essentially a "virtual" (non-existent)
  76. * filesystem.
  77. */
  78. bool null_dev_desc_ok;
  79. int (*probe)(block_dev_desc_t *fs_dev_desc,
  80. disk_partition_t *fs_partition);
  81. int (*ls)(const char *dirname);
  82. int (*exists)(const char *filename);
  83. int (*size)(const char *filename, loff_t *size);
  84. int (*read)(const char *filename, void *buf, loff_t offset,
  85. loff_t len, loff_t *actread);
  86. int (*write)(const char *filename, void *buf, loff_t offset,
  87. loff_t len, loff_t *actwrite);
  88. void (*close)(void);
  89. int (*uuid)(char *uuid_str);
  90. };
  91. static struct fstype_info fstypes[] = {
  92. #ifdef CONFIG_FS_FAT
  93. {
  94. .fstype = FS_TYPE_FAT,
  95. .null_dev_desc_ok = false,
  96. .probe = fat_set_blk_dev,
  97. .close = fat_close,
  98. .ls = file_fat_ls,
  99. .exists = fat_exists,
  100. .size = fat_size,
  101. .read = fat_read_file,
  102. #ifdef CONFIG_FAT_WRITE
  103. .write = file_fat_write,
  104. #else
  105. .write = fs_write_unsupported,
  106. #endif
  107. .uuid = fs_uuid_unsupported,
  108. },
  109. #endif
  110. #ifdef CONFIG_FS_EXT4
  111. {
  112. .fstype = FS_TYPE_EXT,
  113. .null_dev_desc_ok = false,
  114. .probe = ext4fs_probe,
  115. .close = ext4fs_close,
  116. .ls = ext4fs_ls,
  117. .exists = ext4fs_exists,
  118. .size = ext4fs_size,
  119. .read = ext4_read_file,
  120. #ifdef CONFIG_CMD_EXT4_WRITE
  121. .write = ext4_write_file,
  122. #else
  123. .write = fs_write_unsupported,
  124. #endif
  125. .uuid = ext4fs_uuid,
  126. },
  127. #endif
  128. #ifdef CONFIG_SANDBOX
  129. {
  130. .fstype = FS_TYPE_SANDBOX,
  131. .null_dev_desc_ok = true,
  132. .probe = sandbox_fs_set_blk_dev,
  133. .close = sandbox_fs_close,
  134. .ls = sandbox_fs_ls,
  135. .exists = sandbox_fs_exists,
  136. .size = sandbox_fs_size,
  137. .read = fs_read_sandbox,
  138. .write = fs_write_sandbox,
  139. .uuid = fs_uuid_unsupported,
  140. },
  141. #endif
  142. {
  143. .fstype = FS_TYPE_ANY,
  144. .null_dev_desc_ok = true,
  145. .probe = fs_probe_unsupported,
  146. .close = fs_close_unsupported,
  147. .ls = fs_ls_unsupported,
  148. .exists = fs_exists_unsupported,
  149. .size = fs_size_unsupported,
  150. .read = fs_read_unsupported,
  151. .write = fs_write_unsupported,
  152. .uuid = fs_uuid_unsupported,
  153. },
  154. };
  155. static struct fstype_info *fs_get_info(int fstype)
  156. {
  157. struct fstype_info *info;
  158. int i;
  159. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
  160. if (fstype == info->fstype)
  161. return info;
  162. }
  163. /* Return the 'unsupported' sentinel */
  164. return info;
  165. }
  166. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  167. {
  168. struct fstype_info *info;
  169. int part, i;
  170. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  171. static int relocated;
  172. if (!relocated) {
  173. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  174. i++, info++) {
  175. info->probe += gd->reloc_off;
  176. info->close += gd->reloc_off;
  177. info->ls += gd->reloc_off;
  178. info->read += gd->reloc_off;
  179. info->write += gd->reloc_off;
  180. }
  181. relocated = 1;
  182. }
  183. #endif
  184. part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
  185. &fs_partition, 1);
  186. if (part < 0)
  187. return -1;
  188. for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  189. if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  190. fstype != info->fstype)
  191. continue;
  192. if (!fs_dev_desc && !info->null_dev_desc_ok)
  193. continue;
  194. if (!info->probe(fs_dev_desc, &fs_partition)) {
  195. fs_type = info->fstype;
  196. return 0;
  197. }
  198. }
  199. return -1;
  200. }
  201. static void fs_close(void)
  202. {
  203. struct fstype_info *info = fs_get_info(fs_type);
  204. info->close();
  205. fs_type = FS_TYPE_ANY;
  206. }
  207. int fs_uuid(char *uuid_str)
  208. {
  209. struct fstype_info *info = fs_get_info(fs_type);
  210. return info->uuid(uuid_str);
  211. }
  212. int fs_ls(const char *dirname)
  213. {
  214. int ret;
  215. struct fstype_info *info = fs_get_info(fs_type);
  216. ret = info->ls(dirname);
  217. fs_type = FS_TYPE_ANY;
  218. fs_close();
  219. return ret;
  220. }
  221. int fs_exists(const char *filename)
  222. {
  223. int ret;
  224. struct fstype_info *info = fs_get_info(fs_type);
  225. ret = info->exists(filename);
  226. fs_close();
  227. return ret;
  228. }
  229. int fs_size(const char *filename, loff_t *size)
  230. {
  231. int ret;
  232. struct fstype_info *info = fs_get_info(fs_type);
  233. ret = info->size(filename, size);
  234. fs_close();
  235. return ret;
  236. }
  237. int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  238. loff_t *actread)
  239. {
  240. struct fstype_info *info = fs_get_info(fs_type);
  241. void *buf;
  242. int ret;
  243. /*
  244. * We don't actually know how many bytes are being read, since len==0
  245. * means read the whole file.
  246. */
  247. buf = map_sysmem(addr, len);
  248. ret = info->read(filename, buf, offset, len, actread);
  249. unmap_sysmem(buf);
  250. /* If we requested a specific number of bytes, check we got it */
  251. if (ret == 0 && len && *actread != len) {
  252. printf("** Unable to read file %s **\n", filename);
  253. ret = -1;
  254. }
  255. fs_close();
  256. return ret;
  257. }
  258. int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  259. loff_t *actwrite)
  260. {
  261. struct fstype_info *info = fs_get_info(fs_type);
  262. void *buf;
  263. int ret;
  264. buf = map_sysmem(addr, len);
  265. ret = info->write(filename, buf, offset, len, actwrite);
  266. unmap_sysmem(buf);
  267. if (ret < 0 && len != *actwrite) {
  268. printf("** Unable to write file %s **\n", filename);
  269. ret = -1;
  270. }
  271. fs_close();
  272. return ret;
  273. }
  274. int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  275. int fstype)
  276. {
  277. loff_t size;
  278. if (argc != 4)
  279. return CMD_RET_USAGE;
  280. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  281. return 1;
  282. if (fs_size(argv[3], &size) < 0)
  283. return CMD_RET_FAILURE;
  284. setenv_hex("filesize", size);
  285. return 0;
  286. }
  287. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  288. int fstype)
  289. {
  290. unsigned long addr;
  291. const char *addr_str;
  292. const char *filename;
  293. loff_t bytes;
  294. loff_t pos;
  295. loff_t len_read;
  296. int ret;
  297. unsigned long time;
  298. char *ep;
  299. if (argc < 2)
  300. return CMD_RET_USAGE;
  301. if (argc > 7)
  302. return CMD_RET_USAGE;
  303. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  304. return 1;
  305. if (argc >= 4) {
  306. addr = simple_strtoul(argv[3], &ep, 16);
  307. if (ep == argv[3] || *ep != '\0')
  308. return CMD_RET_USAGE;
  309. } else {
  310. addr_str = getenv("loadaddr");
  311. if (addr_str != NULL)
  312. addr = simple_strtoul(addr_str, NULL, 16);
  313. else
  314. addr = CONFIG_SYS_LOAD_ADDR;
  315. }
  316. if (argc >= 5) {
  317. filename = argv[4];
  318. } else {
  319. filename = getenv("bootfile");
  320. if (!filename) {
  321. puts("** No boot file defined **\n");
  322. return 1;
  323. }
  324. }
  325. if (argc >= 6)
  326. bytes = simple_strtoul(argv[5], NULL, 16);
  327. else
  328. bytes = 0;
  329. if (argc >= 7)
  330. pos = simple_strtoul(argv[6], NULL, 16);
  331. else
  332. pos = 0;
  333. time = get_timer(0);
  334. ret = fs_read(filename, addr, pos, bytes, &len_read);
  335. time = get_timer(time);
  336. if (ret < 0)
  337. return 1;
  338. printf("%llu bytes read in %lu ms", len_read, time);
  339. if (time > 0) {
  340. puts(" (");
  341. print_size(div_u64(len_read, time) * 1000, "/s");
  342. puts(")");
  343. }
  344. puts("\n");
  345. setenv_hex("filesize", len_read);
  346. return 0;
  347. }
  348. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  349. int fstype)
  350. {
  351. if (argc < 2)
  352. return CMD_RET_USAGE;
  353. if (argc > 4)
  354. return CMD_RET_USAGE;
  355. if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  356. return 1;
  357. if (fs_ls(argc >= 4 ? argv[3] : "/"))
  358. return 1;
  359. return 0;
  360. }
  361. int file_exists(const char *dev_type, const char *dev_part, const char *file,
  362. int fstype)
  363. {
  364. if (fs_set_blk_dev(dev_type, dev_part, fstype))
  365. return 0;
  366. return fs_exists(file);
  367. }
  368. int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  369. int fstype)
  370. {
  371. unsigned long addr;
  372. const char *filename;
  373. loff_t bytes;
  374. loff_t pos;
  375. loff_t len;
  376. int ret;
  377. unsigned long time;
  378. if (argc < 6 || argc > 7)
  379. return CMD_RET_USAGE;
  380. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  381. return 1;
  382. addr = simple_strtoul(argv[3], NULL, 16);
  383. filename = argv[4];
  384. bytes = simple_strtoul(argv[5], NULL, 16);
  385. if (argc >= 7)
  386. pos = simple_strtoul(argv[6], NULL, 16);
  387. else
  388. pos = 0;
  389. time = get_timer(0);
  390. ret = fs_write(filename, addr, pos, bytes, &len);
  391. time = get_timer(time);
  392. if (ret < 0)
  393. return 1;
  394. printf("%llu bytes written in %lu ms", len, time);
  395. if (time > 0) {
  396. puts(" (");
  397. print_size(div_u64(len, time) * 1000, "/s");
  398. puts(")");
  399. }
  400. puts("\n");
  401. return 0;
  402. }
  403. int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  404. int fstype)
  405. {
  406. int ret;
  407. char uuid[37];
  408. memset(uuid, 0, sizeof(uuid));
  409. if (argc < 3 || argc > 4)
  410. return CMD_RET_USAGE;
  411. if (fs_set_blk_dev(argv[1], argv[2], fstype))
  412. return 1;
  413. ret = fs_uuid(uuid);
  414. if (ret)
  415. return CMD_RET_FAILURE;
  416. if (argc == 4)
  417. setenv(argv[3], uuid);
  418. else
  419. printf("%s\n", uuid);
  420. return CMD_RET_SUCCESS;
  421. }