blk_common.c 2.5 KB

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