byte_swap.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <getopt.h>
  12. #include <unistd.h>
  13. #define NUM_MEM_BLOCK 1
  14. #define FOUR_BYTE_ALIGN 4
  15. #define EIGHT_BYTE_ALIGN 8
  16. #define SIZE_TWO_PBL_CMD 24
  17. #define SUCCESS 0
  18. #define FAILURE -1
  19. #define BYTE_SWAP_32(word) ((((word) & 0xff000000) >> 24)| \
  20. (((word) & 0x00ff0000) >> 8) | \
  21. (((word) & 0x0000ff00) << 8) | \
  22. (((word) & 0x000000ff) << 24))
  23. /*
  24. * Returns:
  25. * SUCCESS, on successful byte swapping.
  26. * FAILURE, on failure.
  27. */
  28. int do_byteswap(FILE *fp)
  29. {
  30. int bytes = 0;
  31. uint32_t upper_byte;
  32. uint32_t lower_byte;
  33. uint32_t pad = 0U;
  34. /* Carries number of Padding bytes to be appended to
  35. * make file size 8 byte aligned.
  36. */
  37. int append_bytes;
  38. int ret = FAILURE;
  39. fseek(fp, 0L, SEEK_END);
  40. bytes = ftell(fp);
  41. append_bytes = EIGHT_BYTE_ALIGN - (bytes % EIGHT_BYTE_ALIGN);
  42. if (append_bytes != 0) {
  43. if (fwrite(&pad, append_bytes, NUM_MEM_BLOCK, fp)
  44. != NUM_MEM_BLOCK) {
  45. printf("%s: Error in appending padding bytes.\n",
  46. __func__);
  47. goto byteswap_err;
  48. }
  49. bytes += append_bytes;
  50. }
  51. rewind(fp);
  52. while (bytes > 0) {
  53. if ((fread(&upper_byte, sizeof(upper_byte), NUM_MEM_BLOCK, fp)
  54. != NUM_MEM_BLOCK) && (feof(fp) == 0)) {
  55. printf("%s: Error reading upper bytes.\n", __func__);
  56. goto byteswap_err;
  57. }
  58. if ((fread(&lower_byte, sizeof(lower_byte), NUM_MEM_BLOCK, fp)
  59. != NUM_MEM_BLOCK) && (feof(fp) == 0)) {
  60. printf("%s: Error reading lower bytes.\n", __func__);
  61. goto byteswap_err;
  62. }
  63. fseek(fp, -8L, SEEK_CUR);
  64. upper_byte = BYTE_SWAP_32(upper_byte);
  65. lower_byte = BYTE_SWAP_32(lower_byte);
  66. if (fwrite(&lower_byte, sizeof(lower_byte), NUM_MEM_BLOCK, fp)
  67. != NUM_MEM_BLOCK) {
  68. printf("%s: Error writing lower bytes.\n", __func__);
  69. goto byteswap_err;
  70. }
  71. if (fwrite(&upper_byte, sizeof(upper_byte), NUM_MEM_BLOCK, fp)
  72. != NUM_MEM_BLOCK) {
  73. printf("%s: Error writing upper bytes.\n", __func__);
  74. goto byteswap_err;
  75. }
  76. bytes -= EIGHT_BYTE_ALIGN;
  77. }
  78. ret = SUCCESS;
  79. byteswap_err:
  80. return ret;
  81. }
  82. int main(int argc, char **argv)
  83. {
  84. FILE *fp = NULL;
  85. int ret = 0;
  86. if (argc > 2) {
  87. printf("Usage format is byteswap <filename>");
  88. return -1;
  89. }
  90. fp = fopen(argv[1], "rb+");
  91. if (fp == NULL) {
  92. printf("%s: Error opening the input file: %s\n",
  93. __func__, argv[1]);
  94. return -1;
  95. }
  96. ret = do_byteswap(fp);
  97. fclose(fp);
  98. return ret;
  99. }