blk_common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Handling of common block commands
  3. *
  4. * Copyright (c) 2017 Google, Inc
  5. *
  6. * (C) Copyright 2000-2011
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <blk.h>
  13. #ifdef HAVE_BLOCK_DEVICE
  14. int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
  15. int *cur_devnump)
  16. {
  17. const char *if_name = blk_get_if_type_name(if_type);
  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(if_type);
  25. return 0;
  26. } else if (strncmp(argv[1], "dev", 3) == 0) {
  27. if (blk_print_device_num(if_type, *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(if_type))
  34. printf("\nno %s devices available\n", 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)simple_strtoul(argv[2], NULL, 10);
  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)simple_strtoul(argv[2], NULL, 10);
  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 = simple_strtoul(argv[2], NULL, 16);
  61. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  62. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  63. ulong n;
  64. printf("\n%s read: device %d block # %lld, count %ld ... ",
  65. if_name, *cur_devnump, (unsigned long long)blk,
  66. cnt);
  67. n = blk_read_devnum(if_type, *cur_devnump, blk, cnt,
  68. (ulong *)addr);
  69. printf("%ld blocks read: %s\n", n,
  70. n == cnt ? "OK" : "ERROR");
  71. return n == cnt ? 0 : 1;
  72. } else if (strcmp(argv[1], "write") == 0) {
  73. ulong addr = simple_strtoul(argv[2], NULL, 16);
  74. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  75. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  76. ulong n;
  77. printf("\n%s write: device %d block # %lld, count %ld ... ",
  78. if_name, *cur_devnump, (unsigned long long)blk,
  79. cnt);
  80. n = blk_write_devnum(if_type, *cur_devnump, blk, cnt,
  81. (ulong *)addr);
  82. printf("%ld blocks written: %s\n", n,
  83. n == cnt ? "OK" : "ERROR");
  84. return n == cnt ? 0 : 1;
  85. } else {
  86. return CMD_RET_USAGE;
  87. }
  88. return 0;
  89. }
  90. }
  91. #endif