blk_common.c 2.6 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. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  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 devices available\n", 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. }
  87. #endif