0001-romfile-implement-a-generic-loader.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. From 2aff8adc1dcd1315877fdb4ac4ef5e649c5b7d11 Mon Sep 17 00:00:00 2001
  2. From: Riku Viitanen <riku.viitanen@protonmail.com>
  3. Date: Sat, 10 Feb 2024 21:23:33 +0200
  4. Subject: [PATCH 1/2] romfile: implement a generic loader
  5. romfile_loadfile_g:
  6. Based on romfile_loadfile but more flexible. User has to supply pointer
  7. to a malloc function and the number of trailing padding bytes. Thus, any
  8. memory region may be used.
  9. romfile_loadfile:
  10. It is now a wrapper around romfile_loadfile_g. Functionality is the same.
  11. Signed-off-by: Riku Viitanen <riku.viitanen@protonmail.com>
  12. ---
  13. src/romfile.c | 25 ++++++++++++++++++++-----
  14. src/romfile.h | 2 ++
  15. 2 files changed, 22 insertions(+), 5 deletions(-)
  16. diff --git a/src/romfile.c b/src/romfile.c
  17. index b598274e..8bf95713 100644
  18. --- a/src/romfile.c
  19. +++ b/src/romfile.c
  20. @@ -47,10 +47,12 @@ romfile_find(const char *name)
  21. return __romfile_findprefix(name, strlen(name) + 1, NULL);
  22. }
  23. -// Helper function to find, malloc_tmphigh, and copy a romfile. This
  24. -// function adds a trailing zero to the malloc'd copy.
  25. +// Generic function to find romfile, malloc (using provided function
  26. +// pointer), and copy a romfile. add_len specifies how many additional
  27. +// trailing bytes to reserve. The extra bytes will not be initialised.
  28. void *
  29. -romfile_loadfile(const char *name, int *psize)
  30. +romfile_loadfile_g(const char *name, int *psize,
  31. + void *(*malloc_fn)(), int add_len)
  32. {
  33. struct romfile_s *file = romfile_find(name);
  34. if (!file)
  35. @@ -60,7 +62,7 @@ romfile_loadfile(const char *name, int *psize)
  36. if (!filesize)
  37. return NULL;
  38. - char *data = malloc_tmphigh(filesize+1);
  39. + char *data = malloc_fn(filesize+add_len);
  40. if (!data) {
  41. warn_noalloc();
  42. return NULL;
  43. @@ -74,7 +76,20 @@ romfile_loadfile(const char *name, int *psize)
  44. }
  45. if (psize)
  46. *psize = filesize;
  47. - data[filesize] = '\0';
  48. +
  49. + return data;
  50. +}
  51. +
  52. +// Helper function to find, malloc_tmphigh, and copy a romfile. This
  53. +// function adds a trailing zero to the malloc'd copy.
  54. +void *
  55. +romfile_loadfile(const char *name, int *psize)
  56. +{
  57. + char *data = romfile_loadfile_g(name, psize, &malloc_tmphigh, 1);
  58. + if (!data)
  59. + return NULL;
  60. +
  61. + data[*psize] = '\0';
  62. return data;
  63. }
  64. diff --git a/src/romfile.h b/src/romfile.h
  65. index 3e0f8200..a320a5bc 100644
  66. --- a/src/romfile.h
  67. +++ b/src/romfile.h
  68. @@ -13,6 +13,8 @@ struct romfile_s {
  69. void romfile_add(struct romfile_s *file);
  70. struct romfile_s *romfile_findprefix(const char *prefix, struct romfile_s *prev);
  71. struct romfile_s *romfile_find(const char *name);
  72. +void *romfile_loadfile_g(const char *name, int *psize,
  73. + void *(*malloc_fn)(), int add_len);
  74. void *romfile_loadfile(const char *name, int *psize);
  75. u64 romfile_loadint(const char *name, u64 defval);
  76. --
  77. 2.43.0