|
| 1 | +/* |
| 2 | + * ELF to firmware image creator. |
| 3 | + * |
| 4 | + * Copyright (c) 2015, Intel Corporation. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms and conditions of the GNU General Public License, |
| 8 | + * version 2, as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | + * more details. |
| 14 | + */ |
| 15 | + |
| 16 | + |
| 17 | +#include <stdlib.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <unistd.h> |
| 20 | +#include <errno.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +#include "rimage.h" |
| 24 | +#include "file_format.h" |
| 25 | + |
| 26 | +/* taken from the linker scripts */ |
| 27 | +static const struct section byt_sections[] = { |
| 28 | + {"ResetVector", 0xff2c0000, 0x2e0}, |
| 29 | + {"ResetVector.literal", 0xff2c02e0, 0x120}, |
| 30 | + {"WindowVectors", 0xff2c0400, 0x178}, |
| 31 | + {"Level2InterruptVector.literal", 0xff2c0578, 0x4}, |
| 32 | + {"Level2InterruptVector", 0xff2c057c, 0x1c}, |
| 33 | + {"Level3InterruptVector.literal", 0xff2c0598, 0x4}, |
| 34 | + {"Level3InterruptVector", 0xff2c059c, 0x1c}, |
| 35 | + {"Level4InterruptVector.literal", 0xff2c05b8, 0x4}, |
| 36 | + {"Level4InterruptVector", 0xff2c05bc, 0x1c}, |
| 37 | + {"Level5InterruptVector.literal", 0xff2c05d8, 0x4}, |
| 38 | + {"Level5InterruptVector", 0xff2c05dc, 0x1c}, |
| 39 | + {"DebugInterruptVector.literal", 0xff2c05d8, 0x4}, |
| 40 | + {"NMIExceptionVector", 0xff2c061c}, |
| 41 | +}; |
| 42 | + |
| 43 | +static const enum reef_module_id modules[] = { |
| 44 | + REEF_MODULE_BASE_FW, |
| 45 | + REEF_MODULE_AAC_5_1, |
| 46 | + REEF_MODULE_PCM, |
| 47 | + REEF_MODULE_PCM_SYSTEM, |
| 48 | + REEF_MODULE_PCM_CAPTURE, |
| 49 | + REEF_MODULE_PCM_REFERENCE, |
| 50 | + REEF_MODULE_BLUETOOTH_RENDER_MODULE, |
| 51 | + REEF_MODULE_BLUETOOTH_CAPTURE_MODULE, |
| 52 | +}; |
| 53 | + |
| 54 | +static int is_iram(struct image *image, Elf32_Shdr *section) |
| 55 | +{ |
| 56 | + const struct adsp *adsp = image->adsp; |
| 57 | + uint32_t start, end; |
| 58 | + |
| 59 | + start = section->sh_addr; |
| 60 | + end = section->sh_addr + section->sh_size; |
| 61 | + |
| 62 | + if (start < adsp->iram_base) |
| 63 | + return 0; |
| 64 | + if (start >= adsp->iram_base + adsp->iram_size) |
| 65 | + return 0; |
| 66 | + return 1; |
| 67 | +} |
| 68 | + |
| 69 | +static int is_dram(struct image *image, Elf32_Shdr *section) |
| 70 | +{ |
| 71 | + const struct adsp *adsp = image->adsp; |
| 72 | + uint32_t start, end; |
| 73 | + |
| 74 | + start = section->sh_addr; |
| 75 | + end = section->sh_addr + section->sh_size; |
| 76 | + |
| 77 | + if (start < adsp->dram_base) |
| 78 | + return 0; |
| 79 | + if (start >= adsp->dram_base + adsp->dram_size) |
| 80 | + return 0; |
| 81 | + return 1; |
| 82 | +} |
| 83 | + |
| 84 | +static int block_idx = 0; |
| 85 | + |
| 86 | +static int write_block(struct image *image, Elf32_Shdr *section) |
| 87 | +{ |
| 88 | + const struct adsp *adsp = image->adsp; |
| 89 | + struct dma_block_info block; |
| 90 | + size_t count; |
| 91 | + void *buffer; |
| 92 | + int ret; |
| 93 | + |
| 94 | + block.size = section->sh_size; |
| 95 | + |
| 96 | + if (is_iram(image, section)) { |
| 97 | + block.type = REEF_IRAM; |
| 98 | + block.ram_offset = section->sh_addr - adsp->iram_base; |
| 99 | + } else if (is_dram(image, section)) { |
| 100 | + block.type = REEF_DRAM; |
| 101 | + block.ram_offset = section->sh_addr - adsp->dram_base; |
| 102 | + } else { |
| 103 | + fprintf(stderr, "error: invalid block address/size 0x%x/0x%x\n", |
| 104 | + section->sh_addr, section->sh_size); |
| 105 | + return -EINVAL; |
| 106 | + } |
| 107 | + |
| 108 | + /* write header */ |
| 109 | + count = fwrite(&block, sizeof(block), 1, image->out_fd); |
| 110 | + if (count != 1) |
| 111 | + return -errno; |
| 112 | + |
| 113 | + /* alloc data data */ |
| 114 | + buffer = calloc(1, section->sh_size); |
| 115 | + if (buffer == NULL) |
| 116 | + return -ENOMEM; |
| 117 | + |
| 118 | + /* read in section data */ |
| 119 | + ret = fseek(image->in_fd, section->sh_offset, SEEK_SET); |
| 120 | + if (ret < 0) { |
| 121 | + fprintf(stderr, "error: cant seek to section %d\n", ret); |
| 122 | + goto out; |
| 123 | + } |
| 124 | + count = fread(buffer, 1, section->sh_size, image->in_fd); |
| 125 | + if (count != section->sh_size) { |
| 126 | + fprintf(stderr, "error: cant read section %d\n", -errno); |
| 127 | + ret = -errno; |
| 128 | + goto out; |
| 129 | + } |
| 130 | + |
| 131 | + /* write out section data */ |
| 132 | + count = fwrite(buffer, 1, section->sh_size, image->out_fd); |
| 133 | + if (count != section->sh_size) { |
| 134 | + fprintf(stderr, "error: cant write section %d\n", -errno); |
| 135 | + fprintf(stderr, " foffset %d size 0x%x mem addr 0x%x\n", |
| 136 | + section->sh_offset, section->sh_size, section->sh_addr); |
| 137 | + ret = -errno; |
| 138 | + goto out; |
| 139 | + } |
| 140 | + |
| 141 | + if (image->verbose) { |
| 142 | + fprintf(stdout, "block: %d\n foffset %d\n size 0x%x\n mem addr 0x%x\n", |
| 143 | + block_idx++, section->sh_offset, section->sh_size, |
| 144 | + section->sh_addr); |
| 145 | + } |
| 146 | + |
| 147 | +out: |
| 148 | + free(buffer); |
| 149 | + return ret; |
| 150 | +} |
| 151 | + |
| 152 | +/* used by others */ |
| 153 | +int byt_write_modules(struct image *image) |
| 154 | +{ |
| 155 | + const struct adsp *adsp = image->adsp; |
| 156 | + struct byt_module_header hdr; |
| 157 | + Elf32_Shdr *section; |
| 158 | + size_t count; |
| 159 | + int i, err; |
| 160 | + uint32_t valid = (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR); |
| 161 | + |
| 162 | + memcpy(hdr.signature, REEF_FW_SIGN, REEF_FW_SIGNATURE_SIZE); |
| 163 | + hdr.blocks = image->num_sections - image->num_bss; |
| 164 | + hdr.mod_size = image->text_size + image->data_size + \ |
| 165 | + sizeof(struct dma_block_info) * hdr.blocks; |
| 166 | + hdr.type = REEF_MODULE_BASE_FW; |
| 167 | + hdr.entry_point = 0;//section->sh_addr; |
| 168 | + |
| 169 | + count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd); |
| 170 | + if (count != 1) { |
| 171 | + fprintf(stderr, "error: failed to write section header %d\n", i); |
| 172 | + return -errno ; |
| 173 | + } |
| 174 | + |
| 175 | + for (i = 0; i < image->hdr.e_shnum; i++) { |
| 176 | + |
| 177 | + section = &image->section[i]; |
| 178 | + |
| 179 | + /* only write valid sections */ |
| 180 | + if (!(image->section[i].sh_flags & valid)) |
| 181 | + continue; |
| 182 | + |
| 183 | + /* dont write bss */ |
| 184 | + if (section->sh_type == SHT_NOBITS) |
| 185 | + continue; |
| 186 | + |
| 187 | + err = write_block(image, section); |
| 188 | + if (err < 0) { |
| 189 | + fprintf(stderr, "error: failed to write section #%d\n", i); |
| 190 | + return err; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /* write the remaining 7 fake modules headers, to make the linux driver happy */ |
| 195 | + hdr.mod_size = 0; |
| 196 | + hdr.blocks = 0; |
| 197 | + |
| 198 | + for (i = 1; i < sizeof(modules) / sizeof(modules[0]); i++) { |
| 199 | + hdr.type = modules[i]; |
| 200 | + count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd); |
| 201 | + if (count != 1) { |
| 202 | + fprintf(stderr, "error: failed to write section header %d\n", i); |
| 203 | + return -errno ; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return 0; |
| 208 | +} |
| 209 | + |
| 210 | +/* used by others */ |
| 211 | +int byt_write_header(struct image *image) |
| 212 | +{ |
| 213 | + struct fw_header hdr; |
| 214 | + size_t count; |
| 215 | + |
| 216 | + memcpy(hdr.signature, REEF_FW_SIGN, REEF_FW_SIGNATURE_SIZE); |
| 217 | + |
| 218 | + hdr.modules = sizeof(modules) / sizeof(modules[0]); |
| 219 | + hdr.file_format = 0; |
| 220 | + |
| 221 | + image->fw_size += sizeof(struct dma_block_info) * |
| 222 | + (image->num_sections - image->num_bss); |
| 223 | + image->fw_size += sizeof(struct byt_module_header) * hdr.modules; |
| 224 | + hdr.file_size = image->fw_size; |
| 225 | + |
| 226 | + fprintf(stdout, "fw: image size %ld (0x%lx) bytes %d modules\n\n", |
| 227 | + hdr.file_size + sizeof(hdr), hdr.file_size + sizeof(hdr), |
| 228 | + hdr.modules); |
| 229 | + count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd); |
| 230 | + if (count != 1) |
| 231 | + return -errno; |
| 232 | + |
| 233 | + return 0; |
| 234 | +} |
| 235 | + |
| 236 | +const struct adsp byt_machine = { |
| 237 | + .name = "byt", |
| 238 | + .iram_base = 0xff2c0000, |
| 239 | + .iram_size = 0x14000, |
| 240 | + .dram_base = 0xff300000, |
| 241 | + .dram_size = 0x28000, |
| 242 | + .image_size = 0xff300000 - 0xff2c0000 + 0x28000, |
| 243 | + .dram_offset = 0xff300000 - 0xff2c0000, |
| 244 | + .machine_id = MACHINE_BAYTRAIL, |
| 245 | + .ops = { |
| 246 | + .write_header = byt_write_header, |
| 247 | + .write_modules = byt_write_modules, |
| 248 | + }, |
| 249 | + .sections = byt_sections, |
| 250 | +}; |
0 commit comments