fs.c 11 KB

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