Peer Chen | 8d782ee | 2011-01-18 21:34:18 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2011 NVIDIA Corporation. All rights reserved. |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * cbootimage.c - Implementation of the cbootimage tool. |
| 25 | */ |
| 26 | |
| 27 | #include "cbootimage.h" |
| 28 | #include "nvbctlib.h" |
| 29 | #include "crypto.h" |
| 30 | #include "data_layout.h" |
| 31 | #include "parse.h" |
| 32 | #include "set.h" |
Anton Staaf | 73664ab | 2011-02-17 09:22:32 -0800 | [diff] [blame] | 33 | #include "context.h" |
Peer Chen | 8d782ee | 2011-01-18 21:34:18 -0500 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Global data |
| 37 | */ |
| 38 | int enable_debug = 0; |
| 39 | |
| 40 | static int help_only = 0; // Only print help & exit |
| 41 | |
| 42 | /* |
| 43 | * Function prototypes |
| 44 | */ |
| 45 | int main(int argc, char *argv[]); |
| 46 | |
Peer Chen | 8d782ee | 2011-01-18 21:34:18 -0500 | [diff] [blame] | 47 | int |
| 48 | write_image_file(build_image_context *context) |
| 49 | { |
| 50 | assert(context != NULL); |
| 51 | |
| 52 | return write_block_raw(context); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | usage(void) |
| 57 | { |
| 58 | printf("Usage: cbootimage [options] configfile imagename\n"); |
| 59 | printf(" options:\n"); |
| 60 | printf(" -h, --help, -? Display this message.\n"); |
| 61 | printf(" -d, --debug Output debugging information.\n"); |
| 62 | printf(" configfile File with configuration information\n"); |
| 63 | printf(" imagename Output image name\n"); |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | process_command_line(int argc, char *argv[], build_image_context *context) |
| 68 | { |
| 69 | int arg = 1; |
| 70 | |
| 71 | while (arg < argc) { |
| 72 | /* Process the next argument. */ |
| 73 | if (!strcmp(argv[arg], "-h") || |
| 74 | !strcmp(argv[arg], "--help") || |
| 75 | !strcmp(argv[arg], "-?")) { |
| 76 | help_only = 1; |
| 77 | usage(); |
| 78 | return 0; |
| 79 | } else if (!strcmp(argv[arg], "-d") || |
| 80 | !strcmp(argv[arg], "--debug")) { |
| 81 | enable_debug = 1; |
| 82 | arg++; |
| 83 | } else if (argv[arg][0] == '-') { |
| 84 | printf("Illegal option %s\n", argv[arg]); |
| 85 | usage(); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | else |
| 89 | break; /* Finished with options */ |
| 90 | } |
| 91 | |
| 92 | /* Handle file specification errors. */ |
| 93 | switch (argc - arg) { |
| 94 | case 0: |
| 95 | printf("Missing configuration and image file names.\n"); |
| 96 | usage(); |
| 97 | return -EINVAL; |
| 98 | |
| 99 | case 1: |
| 100 | printf("Missing configuration or image file name.\n"); |
| 101 | usage(); |
| 102 | return -EINVAL; |
| 103 | |
| 104 | case 2: |
| 105 | /* Correct args, so break from the switch statement. */ |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | printf("Too many arguments.\n"); |
| 110 | usage(); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | /* Open the configuration file. */ |
| 115 | context->config_file = fopen(argv[arg], "r"); |
| 116 | if (context->config_file == NULL) { |
| 117 | printf("Error opening config file.\n"); |
| 118 | return -ENODATA; |
| 119 | } |
| 120 | |
| 121 | /* Record the output filename */ |
| 122 | context->image_filename = argv[arg + 1]; |
| 123 | |
| 124 | /* Set up the Nvbctlib function pointers. */ |
| 125 | nvbct_lib_get_fns(&(context->bctlib)); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int |
| 131 | main(int argc, char *argv[]) |
| 132 | { |
| 133 | int e = 0; |
| 134 | build_image_context context; |
| 135 | u_int32_t data = 0; |
| 136 | |
| 137 | memset(&context, 0, sizeof(build_image_context)); |
| 138 | |
| 139 | /* Process command line arguments. */ |
| 140 | if (process_command_line(argc, argv, &context) != 0) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | if (help_only) |
| 144 | return 1; |
| 145 | |
| 146 | e = init_context(&context); |
| 147 | if (e != 0) { |
| 148 | printf("context initialization failed. Aborting.\n"); |
| 149 | return e; |
| 150 | } |
| 151 | |
| 152 | if (enable_debug) { |
| 153 | /* Debugging information... */ |
| 154 | e = context.bctlib.get_value(nvbct_lib_id_bct_size, |
| 155 | &data, context.bct); |
| 156 | printf("bct size: %d\n", e == 0 ? data : -1); |
| 157 | } |
| 158 | |
| 159 | /* Open the raw output file. */ |
| 160 | context.raw_file = fopen(context.image_filename, |
| 161 | "w+"); |
| 162 | if (context.raw_file == NULL) { |
| 163 | printf("Error opening raw file %s.\n", |
| 164 | context.image_filename); |
| 165 | goto fail; |
| 166 | } |
| 167 | |
| 168 | /* Parse & process the contents of the config file. */ |
| 169 | process_config_file(&context); |
| 170 | |
| 171 | /* Update the bct file */ |
| 172 | /* Update the add on file */ |
| 173 | e = update_addon_item(&context); |
| 174 | if ( e!= 0) { |
| 175 | printf("Write addon item failed, error: %d.\n", e); |
| 176 | goto fail; |
| 177 | } |
| 178 | /* Peform final signing & encryption of bct. */ |
| 179 | e = sign_bct(&context, context.bct); |
| 180 | if (e != 0) { |
| 181 | printf("Signing BCT failed, error: %d.\n", e); |
| 182 | goto fail; |
| 183 | } |
| 184 | /* Write the image file. */ |
| 185 | /* The image hasn't been written yet. */ |
| 186 | if (write_image_file(&context) != 0) |
| 187 | printf("Error writing image file.\n"); |
| 188 | else |
| 189 | printf("Image file %s has been successfully generated!\n", |
| 190 | context.image_filename); |
| 191 | |
| 192 | fail: |
| 193 | /* Close the file(s). */ |
| 194 | if (context.raw_file) |
| 195 | fclose(context.raw_file); |
| 196 | |
| 197 | /* Clean up memory. */ |
| 198 | cleanup_context(&context); |
| 199 | |
| 200 | return e; |
| 201 | } |