blk_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. int blk_common_cmd(int argc, char *const argv[], enum if_type if_type,
  14. int *cur_devnump)
  15. {
  16. const char *if_name = blk_get_if_type_name(if_type);
  17. switch (argc) {
  18. case 0:
  19. case 1:
  20. return CMD_RET_USAGE;
  21. case 2:
  22. if (strncmp(argv[1], "inf", 3) == 0) {
  23. blk_list_devices(if_type);
  24. return 0;
  25. } else if (strncmp(argv[1], "dev", 3) == 0) {
  26. if (blk_print_device_num(if_type, *cur_devnump)) {
  27. printf("\nno %s devices available\n", if_name);
  28. return CMD_RET_FAILURE;
  29. }
  30. return 0;
  31. } else if (strncmp(argv[1], "part", 4) == 0) {
  32. if (blk_list_part(if_type))
  33. printf("\nno %s partition table available\n",
  34. if_name);
  35. return 0;
  36. }
  37. return CMD_RET_USAGE;
  38. case 3:
  39. if (strncmp(argv[1], "dev", 3) == 0) {
  40. int dev = (int)dectoul(argv[2], NULL);
  41. if (!blk_show_device(if_type, dev)) {
  42. *cur_devnump = dev;
  43. printf("... is now current device\n");
  44. } else {
  45. return CMD_RET_FAILURE;
  46. }
  47. return 0;
  48. } else if (strncmp(argv[1], "part", 4) == 0) {
  49. int dev = (int)dectoul(argv[2], NULL);
  50. if (blk_print_part_devnum(if_type, dev)) {
  51. printf("\n%s device %d not available\n",
  52. if_name, dev);
  53. return CMD_RET_FAILURE;
  54. }
  55. return 0;
  56. }
  57. return CMD_RET_USAGE;
  58. default: /* at least 4 args */
  59. if (strcmp(argv[1], "read") == 0) {
  60. ulong addr = hextoul(argv[2], NULL);
  61. lbaint_t blk = hextoul(argv[3], NULL);
  62. ulong cnt = hextoul(argv[4], NULL);
  63. ulong n;
  64. printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
  65. if_name, *cur_devnump, blk, cnt);
  66. n = blk_read_devnum(if_type, *cur_devnump, blk, cnt,
  67. (ulong *)addr);
  68. printf("%ld blocks read: %s\n", n,
  69. n == cnt ? "OK" : "ERROR");
  70. return n == cnt ? 0 : 1;
  71. } else if (strcmp(argv[1], "write") == 0) {
  72. ulong addr = hextoul(argv[2], NULL);
  73. lbaint_t blk = hextoul(argv[3], NULL);
  74. ulong cnt = hextoul(argv[4], NULL);
  75. ulong n;
  76. printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
  77. if_name, *cur_devnump, blk, cnt);
  78. n = blk_write_devnum(if_type, *cur_devnump, blk, cnt,
  79. (ulong *)addr);
  80. printf("%ld blocks written: %s\n", n,
  81. n == cnt ? "OK" : "ERROR");
  82. return n == cnt ? 0 : 1;
  83. } else {
  84. return CMD_RET_USAGE;
  85. }
  86. }
  87. }