100-fw_env-make-flash_io-take-buffer-as-an-argument.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. From f178f7c9550c4fd9c644f79a1eb2dafa5bcdce25 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <[email protected]>
  3. Date: Wed, 12 Jan 2022 12:47:05 +0100
  4. Subject: [PATCH] fw_env: make flash_io() take buffer as an argument
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. It's usually easier to understand code & follow it if all arguments are
  9. passed explicitly. Many coding styles also discourage using global
  10. variables.
  11. Behaviour of flash_io() was a bit unintuitive as it was writing to a
  12. buffer referenced in a global struct. That required developers to
  13. remember how it works and sometimes required hacking "environment"
  14. global struct variable to read data into a proper buffer.
  15. Signed-off-by: Rafał Miłecki <[email protected]>
  16. ---
  17. tools/env/fw_env.c | 32 ++++++++++++++++----------------
  18. 1 file changed, 16 insertions(+), 16 deletions(-)
  19. --- a/tools/env/fw_env.c
  20. +++ b/tools/env/fw_env.c
  21. @@ -346,7 +346,7 @@ static int ubi_write(int fd, const void
  22. return 0;
  23. }
  24. -static int flash_io(int mode);
  25. +static int flash_io(int mode, void *buf, size_t count);
  26. static int parse_config(struct env_opts *opts);
  27. #if defined(CONFIG_FILE)
  28. @@ -516,7 +516,7 @@ int fw_env_flush(struct env_opts *opts)
  29. *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
  30. /* write environment back to flash */
  31. - if (flash_io(O_RDWR)) {
  32. + if (flash_io(O_RDWR, environment.image, CUR_ENVSIZE)) {
  33. fprintf(stderr, "Error: can't write fw_env to flash\n");
  34. return -1;
  35. }
  36. @@ -1185,7 +1185,8 @@ static int flash_flag_obsolete(int dev,
  37. return rc;
  38. }
  39. -static int flash_write(int fd_current, int fd_target, int dev_target)
  40. +static int flash_write(int fd_current, int fd_target, int dev_target, void *buf,
  41. + size_t count)
  42. {
  43. int rc;
  44. @@ -1212,11 +1213,10 @@ static int flash_write(int fd_current, i
  45. if (IS_UBI(dev_target)) {
  46. if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
  47. return -1;
  48. - return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
  49. + return ubi_write(fd_target, buf, count);
  50. }
  51. - rc = flash_write_buf(dev_target, fd_target, environment.image,
  52. - CUR_ENVSIZE);
  53. + rc = flash_write_buf(dev_target, fd_target, buf, count);
  54. if (rc < 0)
  55. return rc;
  56. @@ -1235,17 +1235,17 @@ static int flash_write(int fd_current, i
  57. return 0;
  58. }
  59. -static int flash_read(int fd)
  60. +static int flash_read(int fd, void *buf, size_t count)
  61. {
  62. int rc;
  63. if (IS_UBI(dev_current)) {
  64. DEVTYPE(dev_current) = MTD_ABSENT;
  65. - return ubi_read(fd, environment.image, CUR_ENVSIZE);
  66. + return ubi_read(fd, buf, count);
  67. }
  68. - rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
  69. + rc = flash_read_buf(dev_current, fd, buf, count,
  70. DEVOFFSET(dev_current));
  71. if (rc != CUR_ENVSIZE)
  72. return -1;
  73. @@ -1291,7 +1291,7 @@ err:
  74. return rc;
  75. }
  76. -static int flash_io_write(int fd_current)
  77. +static int flash_io_write(int fd_current, void *buf, size_t count)
  78. {
  79. int fd_target = -1, rc, dev_target;
  80. const char *dname, *target_temp = NULL;
  81. @@ -1322,7 +1322,7 @@ static int flash_io_write(int fd_current
  82. fd_target = fd_current;
  83. }
  84. - rc = flash_write(fd_current, fd_target, dev_target);
  85. + rc = flash_write(fd_current, fd_target, dev_target, buf, count);
  86. if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
  87. fprintf(stderr,
  88. @@ -1377,7 +1377,7 @@ static int flash_io_write(int fd_current
  89. return rc;
  90. }
  91. -static int flash_io(int mode)
  92. +static int flash_io(int mode, void *buf, size_t count)
  93. {
  94. int fd_current, rc;
  95. @@ -1391,9 +1391,9 @@ static int flash_io(int mode)
  96. }
  97. if (mode == O_RDWR) {
  98. - rc = flash_io_write(fd_current);
  99. + rc = flash_io_write(fd_current, buf, count);
  100. } else {
  101. - rc = flash_read(fd_current);
  102. + rc = flash_read(fd_current, buf, count);
  103. }
  104. if (close(fd_current)) {
  105. @@ -1455,7 +1455,7 @@ int fw_env_open(struct env_opts *opts)
  106. }
  107. dev_current = 0;
  108. - if (flash_io(O_RDONLY)) {
  109. + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) {
  110. ret = -EIO;
  111. goto open_cleanup;
  112. }
  113. @@ -1490,7 +1490,7 @@ int fw_env_open(struct env_opts *opts)
  114. * other pointers in environment still point inside addr0
  115. */
  116. environment.image = addr1;
  117. - if (flash_io(O_RDONLY)) {
  118. + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) {
  119. ret = -EIO;
  120. goto open_cleanup;
  121. }