read.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. *
  6. * Alternatively, this software may be distributed under the terms of the
  7. * GNU General Public License ("GPL") version 2 as published by the Free
  8. * Software Foundation.
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <mapmem.h>
  13. #include <part.h>
  14. static int
  15. do_rw(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  16. {
  17. struct blk_desc *dev_desc = NULL;
  18. struct disk_partition part_info;
  19. ulong offset, limit;
  20. uint blk, cnt, res;
  21. void *addr;
  22. int part;
  23. if (argc != 6) {
  24. cmd_usage(cmdtp);
  25. return 1;
  26. }
  27. part = part_get_info_by_dev_and_name_or_num(argv[1], argv[2],
  28. &dev_desc, &part_info, 1);
  29. if (part < 0)
  30. return 1;
  31. addr = map_sysmem(hextoul(argv[3], NULL), 0);
  32. blk = hextoul(argv[4], NULL);
  33. cnt = hextoul(argv[5], NULL);
  34. if (part > 0) {
  35. offset = part_info.start;
  36. limit = part_info.size;
  37. } else {
  38. /* Largest address not available in struct blk_desc. */
  39. offset = 0;
  40. limit = ~0;
  41. }
  42. if (cnt + blk > limit) {
  43. printf("%s out of range\n", cmdtp->name);
  44. return 1;
  45. }
  46. if (IS_ENABLED(CONFIG_CMD_WRITE) && !strcmp(cmdtp->name, "write"))
  47. res = blk_dwrite(dev_desc, offset + blk, cnt, addr);
  48. else
  49. res = blk_dread(dev_desc, offset + blk, cnt, addr);
  50. if (res != cnt) {
  51. printf("%s error\n", cmdtp->name);
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. #ifdef CONFIG_CMD_READ
  57. U_BOOT_CMD(
  58. read, 6, 0, do_rw,
  59. "Load binary data from a partition",
  60. "<interface> <dev[:part|#partname]> addr blk# cnt"
  61. );
  62. #endif
  63. #ifdef CONFIG_CMD_WRITE
  64. U_BOOT_CMD(
  65. write, 6, 0, do_rw,
  66. "Store binary data to a partition",
  67. "<interface> <dev[:part|#partname]> addr blk# cnt"
  68. );
  69. #endif