blk_common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Handling of common block commands
  4. *
  5. * Copyright (c) 2017 Google, Inc
  6. *
  7. * (C) Copyright 2000-2011
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #include <common.h>
  11. #include <blk.h>
  12. #include <command.h>
  13. #include <mapmem.h>
  14. int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
  15. int *cur_devnump)
  16. {
  17. const char *if_name = blk_get_uclass_name(uclass_id);
  18. switch (argc) {
  19. case 0:
  20. case 1:
  21. return CMD_RET_USAGE;
  22. case 2:
  23. if (strncmp(argv[1], "inf", 3) == 0) {
  24. blk_list_devices(uclass_id);
  25. return 0;
  26. } else if (strncmp(argv[1], "dev", 3) == 0) {
  27. if (blk_print_device_num(uclass_id, *cur_devnump)) {
  28. printf("\nno %s devices available\n", if_name);
  29. return CMD_RET_FAILURE;
  30. }
  31. return 0;
  32. } else if (strncmp(argv[1], "part", 4) == 0) {
  33. if (blk_list_part(uclass_id))
  34. printf("\nno %s partition table available\n",
  35. if_name);
  36. return 0;
  37. }
  38. return CMD_RET_USAGE;
  39. case 3:
  40. if (strncmp(argv[1], "dev", 3) == 0) {
  41. int dev = (int)dectoul(argv[2], NULL);
  42. if (!blk_show_device(uclass_id, dev)) {
  43. *cur_devnump = dev;
  44. printf("... is now current device\n");
  45. } else {
  46. return CMD_RET_FAILURE;
  47. }
  48. return 0;
  49. } else if (strncmp(argv[1], "part", 4) == 0) {
  50. int dev = (int)dectoul(argv[2], NULL);
  51. if (blk_print_part_devnum(uclass_id, dev)) {
  52. printf("\n%s device %d not available\n",
  53. if_name, dev);
  54. return CMD_RET_FAILURE;
  55. }
  56. return 0;
  57. }
  58. return CMD_RET_USAGE;
  59. default: /* at least 4 args */
  60. if (strcmp(argv[1], "read") == 0) {
  61. phys_addr_t paddr = hextoul(argv[2], NULL);
  62. lbaint_t blk = hextoul(argv[3], NULL);
  63. ulong cnt = hextoul(argv[4], NULL);
  64. void *vaddr;
  65. ulong n;
  66. printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
  67. if_name, *cur_devnump, blk, cnt);
  68. vaddr = map_sysmem(paddr, 512 * cnt);
  69. n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt,
  70. vaddr);
  71. unmap_sysmem(vaddr);
  72. printf("%ld blocks read: %s\n", n,
  73. n == cnt ? "OK" : "ERROR");
  74. return n == cnt ? 0 : 1;
  75. } else if (strcmp(argv[1], "write") == 0) {
  76. phys_addr_t paddr = hextoul(argv[2], NULL);
  77. lbaint_t blk = hextoul(argv[3], NULL);
  78. ulong cnt = hextoul(argv[4], NULL);
  79. void *vaddr;
  80. ulong n;
  81. printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
  82. if_name, *cur_devnump, blk, cnt);
  83. vaddr = map_sysmem(paddr, 512 * cnt);
  84. n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt,
  85. vaddr);
  86. unmap_sysmem(vaddr);
  87. printf("%ld blocks written: %s\n", n,
  88. n == cnt ? "OK" : "ERROR");
  89. return n == cnt ? 0 : 1;
  90. } else {
  91. return CMD_RET_USAGE;
  92. }
  93. }
  94. }