220-cmd-env-readmem.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --- a/cmd/Kconfig
  2. +++ b/cmd/Kconfig
  3. @@ -622,6 +622,12 @@ config CMD_ENV_EXISTS
  4. Check if a variable is defined in the environment for use in
  5. shell scripting.
  6. +config CMD_ENV_READMEM
  7. + bool "env readmem"
  8. + default y
  9. + help
  10. + Store memory content into environment variable.
  11. +
  12. config CMD_ENV_CALLBACK
  13. bool "env callbacks - print callbacks and their associated variables"
  14. help
  15. --- a/cmd/nvedit.c
  16. +++ b/cmd/nvedit.c
  17. @@ -385,6 +385,60 @@ int do_env_ask(struct cmd_tbl *cmdtp, in
  18. }
  19. #endif
  20. +#if defined(CONFIG_CMD_ENV_READMEM)
  21. +int do_env_readmem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  22. +{
  23. + char varstr[CONFIG_SYS_CBSIZE];
  24. + const void *buf;
  25. + char *local_args[4];
  26. + ulong addr, bytes = 6;
  27. + int hexdump = 0;
  28. +
  29. + /*
  30. + * Check the syntax:
  31. + *
  32. + * readmem [-b] name address [size]
  33. + */
  34. + if (argc < 3)
  35. + return CMD_RET_USAGE;
  36. +
  37. + local_args[0] = argv[0];
  38. +
  39. + if (!strncmp(argv[1], "-b", 3))
  40. + hexdump = 1;
  41. +
  42. + local_args[1] = argv[hexdump + 1];
  43. + local_args[2] = varstr;
  44. + local_args[3] = NULL;
  45. +
  46. + addr = simple_strtoul(argv[hexdump + 2], NULL, 16);
  47. +
  48. + if (!hexdump)
  49. + bytes = simple_strtoul(argv[hexdump + 3], NULL, 16);
  50. +
  51. + if (bytes < 1)
  52. + return 1;
  53. +
  54. + if ((hexdump * 3) * bytes >= CONFIG_SYS_CBSIZE)
  55. + return 1;
  56. +
  57. + buf = map_sysmem(addr, bytes);
  58. + if (!buf)
  59. + return 1;
  60. +
  61. + if (hexdump) {
  62. + sprintf(varstr, "%pM", buf);
  63. + } else {
  64. + memcpy(varstr, buf, bytes);
  65. + varstr[bytes] = '\0';
  66. + }
  67. + unmap_sysmem(buf);
  68. +
  69. + /* Continue calling setenv code */
  70. + return _do_env_set(flag, 3, local_args, H_INTERACTIVE);
  71. +}
  72. +#endif
  73. +
  74. #if defined(CONFIG_CMD_ENV_CALLBACK)
  75. static int print_static_binding(const char *var_name, const char *callback_name,
  76. void *priv)
  77. @@ -1201,6 +1255,9 @@ static struct cmd_tbl cmd_env_sub[] = {
  78. U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
  79. #endif
  80. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  81. +#if defined(CONFIG_CMD_ENV_READMEM)
  82. + U_BOOT_CMD_MKENT(readmem, CONFIG_SYS_MAXARGS, 3, do_env_readmem, "", ""),
  83. +#endif
  84. #if defined(CONFIG_CMD_RUN)
  85. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  86. #endif
  87. @@ -1284,6 +1341,9 @@ U_BOOT_LONGHELP(env,
  88. #if defined(CONFIG_CMD_NVEDIT_EFI)
  89. "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
  90. #endif
  91. +#if defined(CONFIG_CMD_ENV_READMEM)
  92. + "env readmem [-b] name address size - read variable from memory\n"
  93. +#endif
  94. #if defined(CONFIG_CMD_RUN)
  95. "env run var [...] - run commands in an environment variable\n"
  96. #endif
  97. @@ -1392,6 +1452,17 @@ U_BOOT_CMD(
  98. );
  99. #endif
  100. +#if defined(CONFIG_CMD_ENV_READMEM)
  101. +U_BOOT_CMD_COMPLETE(
  102. + readmem, CONFIG_SYS_MAXARGS, 3, do_env_readmem,
  103. + "get environment variable from memory address",
  104. + "name [-b] address size\n"
  105. + " - store memory address to env variable\n"
  106. + " \"-b\": read binary ethaddr",
  107. + var_complete
  108. +);
  109. +#endif
  110. +
  111. #if defined(CONFIG_CMD_RUN)
  112. U_BOOT_CMD_COMPLETE(
  113. run, CONFIG_SYS_MAXARGS, 1, do_run,