Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

elf_loader: add generic callback-based loader

Change-Id: I1dd972a585bc7c805e32c9665d13e248663ccc73

+35 -16
+26 -16
firmware/elf_loader.c
··· 101 101 return ELF_ERR_MEM_UNMAPPED; 102 102 } 103 103 104 - int elf_loadfd(int fd, 105 - const struct elf_load_context *ctx, 106 - void **entrypoint) 104 + int elf_load(elf_read_callback_t read_cb, 105 + intptr_t read_arg, 106 + const struct elf_load_context *ctx, 107 + void **entrypoint) 107 108 { 108 109 struct elf_header ehdr; 109 110 struct elf_phdr phdr; 110 - ssize_t nread; 111 111 int err; 112 112 113 - nread = read(fd, &ehdr, sizeof(ehdr)); 114 - if (nread != (ssize_t)sizeof(ehdr)) 113 + if (read_cb(read_arg, 0, &ehdr, sizeof(ehdr))) 115 114 return ELF_ERR_IO; 116 115 117 116 err = elf_validate_header(&ehdr); ··· 124 123 for (size_t ph_index = 0; ph_index < ehdr.e_phnum; ++ph_index) 125 124 { 126 125 off_t ph_off = ehdr.e_phoff + (ph_index * ehdr.e_phentsize); 127 - if (lseek(fd, ph_off, SEEK_SET) == (off_t)-1) 128 - return ELF_ERR_IO; 129 - 130 - nread = read(fd, &phdr, sizeof(phdr)); 131 - if (nread != (ssize_t)sizeof(phdr)) 126 + if (read_cb(read_arg, ph_off, &phdr, sizeof(phdr))) 132 127 return ELF_ERR_IO; 133 128 134 129 err = elf_validate_phdr(&phdr, ctx); ··· 138 133 /* Load file data to memory if needed */ 139 134 if (phdr.p_filesz > 0) 140 135 { 141 - if (lseek(fd, phdr.p_offset, SEEK_SET) == (off_t)-1) 142 - return ELF_ERR_IO; 143 - 144 - nread = read(fd, (void *)phdr.p_vaddr, phdr.p_filesz); 145 - if (nread < 0 || (uint32_t)nread != phdr.p_filesz) 136 + if (read_cb(read_arg, phdr.p_offset, (void *)phdr.p_vaddr, phdr.p_filesz)) 146 137 return ELF_ERR_IO; 147 138 } 148 139 ··· 158 149 159 150 *entrypoint = (void *)ehdr.e_entry; 160 151 return ELF_OK; 152 + } 153 + 154 + int elf_loadfd(int fd, 155 + const struct elf_load_context *ctx, 156 + void **entrypoint) 157 + { 158 + return elf_load(elf_read_fd_callback, fd, ctx, entrypoint); 159 + } 160 + 161 + int elf_read_fd_callback(intptr_t fd, off_t pos, void *buf, size_t size) 162 + { 163 + if (lseek(fd, pos, SEEK_SET) == (off_t)-1) 164 + return -1; 165 + 166 + ssize_t nread = read(fd, buf, size); 167 + if (nread < 0 || (size_t)nread != size) 168 + return -1; 169 + 170 + return 0; 161 171 } 162 172 163 173 int elf_loadpath(const char *filename,
+9
firmware/include/elf_loader.h
··· 38 38 39 39 #include <stdint.h> 40 40 #include <stddef.h> 41 + #include <sys/types.h> 41 42 42 43 enum elf_error 43 44 { ··· 83 84 size_t num_mmap; 84 85 }; 85 86 87 + typedef int (*elf_read_callback_t) (intptr_t arg, off_t pos, void *buf, size_t size); 88 + 89 + int elf_load(elf_read_callback_t read_cb, 90 + intptr_t read_arg, 91 + const struct elf_load_context *ctx, 92 + void **entrypoint); 93 + 86 94 int elf_loadfd(int fd, 87 95 const struct elf_load_context *ctx, 88 96 void **entrypoint); 97 + int elf_read_fd_callback(intptr_t fd, off_t pos, void *buf, size_t size); 89 98 90 99 int elf_loadpath(const char *filename, 91 100 const struct elf_load_context *ctx,