blk_common.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 partition table available\n",
  33. if_name);
  34. return 0;
  35. }
  36. return CMD_RET_USAGE;
  37. case 3:
  38. if (strncmp(argv[1], "dev", 3) == 0) {
  39. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  40. if (!blk_show_device(if_type, dev)) {
  41. *cur_devnump = dev;
  42. printf("... is now current device\n");
  43. } else {
  44. return CMD_RET_FAILURE;
  45. }
  46. return 0;
  47. } else if (strncmp(argv[1], "part", 4) == 0) {
  48. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  49. if (blk_print_part_devnum(if_type, dev)) {
  50. printf("\n%s device %d not available\n",
  51. if_name, dev);
  52. return CMD_RET_FAILURE;
  53. }
  54. return 0;
  55. }
  56. return CMD_RET_USAGE;
  57. default: /* at least 4 args */
  58. if (strcmp(argv[1], "read") == 0) {
  59. ulong addr = simple_strtoul(argv[2], NULL, 16);
  60. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  61. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  62. ulong n;
  63. printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
  64. if_name, *cur_devnump, blk, cnt);
  65. n = blk_read_devnum(if_type, *cur_devnump, blk, cnt,
  66. (ulong *)addr);
  67. printf("%ld blocks read: %s\n", n,
  68. n == cnt ? "OK" : "ERROR");
  69. return n == cnt ? 0 : 1;
  70. } else if (strcmp(argv[1], "write") == 0) {
  71. ulong addr = simple_strtoul(argv[2], NULL, 16);
  72. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  73. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  74. ulong n;
  75. printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
  76. if_name, *cur_devnump, blk, cnt);
  77. n = blk_write_devnum(if_type, *cur_devnump, blk, cnt,
  78. (ulong *)addr);
  79. printf("%ld blocks written: %s\n", n,
  80. n == cnt ? "OK" : "ERROR");
  81. return n == cnt ? 0 : 1;
  82. } else {
  83. return CMD_RET_USAGE;
  84. }
  85. }
  86. }