fs.c 11 KB

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