Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 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 | #include <stdlib.h> |
| 17 | #include <stdio.h> |
| 18 | #include <unistd.h> |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "rimage.h" |
| 23 | #include "file_format.h" |
| 24 | #include "manifest.h" |
| 25 | |
| 26 | static const struct adsp *machine[] = { |
| 27 | &machine_byt, |
| 28 | &machine_cht, |
| 29 | &machine_bsw, |
| 30 | &machine_hsw, |
| 31 | &machine_bdw, |
| 32 | &machine_apl, |
| 33 | &machine_cnl, |
| 34 | }; |
| 35 | |
| 36 | static void usage(char *name) |
| 37 | { |
Liam Girdwood | 7db25d8 | 2018-04-12 21:07:35 +0100 | [diff] [blame] | 38 | fprintf(stdout, "%s:\t -m machine -o outfile -k [key] ELF files\n", |
| 39 | name); |
Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 40 | fprintf(stdout, "\t -v enable verbose output\n"); |
Liam Girdwood | 7db25d8 | 2018-04-12 21:07:35 +0100 | [diff] [blame] | 41 | fprintf(stdout, "\t -r enable relocatable ELF files\n"); |
Tomasz Lauda | ac7b0c2 | 2018-06-04 12:03:42 +0100 | [diff] [blame] | 42 | fprintf(stdout, "\t -s MEU signing offset\n"); |
Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 43 | exit(0); |
| 44 | } |
| 45 | |
| 46 | int main(int argc, char *argv[]) |
| 47 | { |
| 48 | struct image image; |
| 49 | const char *mach = NULL; |
| 50 | int opt, ret, i, elf_argc = 0; |
| 51 | |
| 52 | memset(&image, 0, sizeof(image)); |
| 53 | |
Tomasz Lauda | ac7b0c2 | 2018-06-04 12:03:42 +0100 | [diff] [blame] | 54 | while ((opt = getopt(argc, argv, "ho:m:vba:s:k:l:r")) != -1) { |
Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 55 | switch (opt) { |
| 56 | case 'o': |
| 57 | image.out_file = optarg; |
| 58 | break; |
| 59 | case 'm': |
| 60 | mach = optarg; |
| 61 | break; |
| 62 | case 'v': |
| 63 | image.verbose = 1; |
| 64 | break; |
| 65 | case 's': |
Tomasz Lauda | ac7b0c2 | 2018-06-04 12:03:42 +0100 | [diff] [blame] | 66 | image.meu_offset = atoi(optarg); |
Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 67 | break; |
| 68 | case 'a': |
| 69 | image.abi = atoi(optarg); |
| 70 | break; |
| 71 | case 'k': |
| 72 | image.key_name = optarg; |
| 73 | break; |
Liam Girdwood | 7db25d8 | 2018-04-12 21:07:35 +0100 | [diff] [blame] | 74 | case 'r': |
| 75 | image.reloc = 1; |
| 76 | break; |
Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 77 | case 'h': |
| 78 | usage(argv[0]); |
| 79 | break; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | elf_argc = optind; |
| 86 | |
| 87 | /* make sure we have an outfile and machine */ |
| 88 | if (image.out_file == NULL || mach == NULL) |
| 89 | usage(argv[0]); |
| 90 | |
| 91 | |
| 92 | /* find machine */ |
| 93 | for (i = 0; i < ARRAY_SIZE(machine); i++) { |
| 94 | if (!strcmp(mach, machine[i]->name)) { |
| 95 | image.adsp = machine[i]; |
| 96 | goto found; |
| 97 | } |
| 98 | } |
| 99 | fprintf(stderr, "error: machine %s not found\n", mach); |
| 100 | fprintf(stderr, "error: available machines "); |
| 101 | for (i = 0; i < ARRAY_SIZE(machine); i++) |
| 102 | fprintf(stderr, "%s, ", machine[i]->name); |
| 103 | fprintf(stderr, "\n"); |
| 104 | |
| 105 | return -EINVAL; |
| 106 | |
| 107 | found: |
| 108 | |
| 109 | /* parse input ELF files */ |
| 110 | image.num_modules = argc - elf_argc; |
| 111 | for (i = elf_argc; i < argc; i++) { |
| 112 | fprintf(stdout, "\nModule Reading %s\n", argv[i]); |
| 113 | ret = elf_parse_module(&image, i - elf_argc, argv[i]); |
| 114 | if (ret < 0) |
| 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | /* validate all modules */ |
| 119 | ret = elf_validate_modules(&image); |
| 120 | if (ret < 0) |
| 121 | goto out; |
| 122 | |
| 123 | /* open outfile for writing */ |
| 124 | unlink(image.out_file); |
| 125 | image.out_fd = fopen(image.out_file, "w"); |
| 126 | if (image.out_fd == NULL) { |
| 127 | fprintf(stderr, "error: unable to open %s for writing %d\n", |
| 128 | image.out_file, errno); |
| 129 | ret = -EINVAL; |
| 130 | goto out; |
| 131 | } |
| 132 | |
| 133 | /* process and write output */ |
Tomasz Lauda | ac7b0c2 | 2018-06-04 12:03:42 +0100 | [diff] [blame] | 134 | if (image.meu_offset) |
| 135 | ret = image.adsp->write_firmware_meu(&image); |
| 136 | else |
| 137 | ret = image.adsp->write_firmware(&image); |
Liam Girdwood | 05ef434 | 2018-02-13 20:29:40 +0000 | [diff] [blame] | 138 | out: |
| 139 | /* close files */ |
| 140 | if (image.out_fd) |
| 141 | fclose(image.out_fd); |
| 142 | |
| 143 | |
| 144 | return ret; |
| 145 | } |