Joel Stanley | 7b62ce9 | 2016-10-31 17:27:13 +1030 | [diff] [blame] | 1 | /* Copyright 2016 IBM Corp. |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | * implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | #include <byteswap.h> |
| 26 | #include <stdint.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <getopt.h> |
| 29 | #include <limits.h> |
| 30 | #include <arpa/inet.h> |
| 31 | #include <assert.h> |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 32 | #include <libflash/arch_flash.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 33 | #include <libflash/libffs.h> |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 34 | #include <libflash/blocklevel.h> |
| 35 | #include <libflash/errors.h> |
Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 36 | #include <openbmc_intf.h> |
| 37 | #include <openbmc.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 38 | |
| 39 | static const gchar* dbus_object_path = "/org/openbmc/control"; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 40 | static const gchar* dbus_name = "org.openbmc.control.Flasher"; |
| 41 | |
| 42 | static GDBusObjectManagerServer *manager = NULL; |
| 43 | |
| 44 | #define __aligned(x) __attribute__((aligned(x))) |
| 45 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 46 | static bool need_relock; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 47 | |
| 48 | #define FILE_BUF_SIZE 0x10000 |
| 49 | static uint8_t file_buf[FILE_BUF_SIZE] __aligned(0x1000); |
| 50 | |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 51 | static struct blocklevel_device *bl; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 52 | static struct ffs_handle *ffsh; |
| 53 | static uint32_t fl_total_size, fl_erase_granule; |
| 54 | static const char *fl_name; |
| 55 | static int32_t ffs_index = -1; |
| 56 | |
| 57 | static uint8_t FLASH_OK = 0; |
| 58 | static uint8_t FLASH_ERROR = 0x01; |
| 59 | static uint8_t FLASH_SETUP_ERROR = 0x02; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 60 | |
| 61 | static int |
| 62 | erase_chip(void) |
| 63 | { |
| 64 | int rc = 0; |
| 65 | |
| 66 | printf("Erasing... (may take a while !) "); |
| 67 | fflush(stdout); |
| 68 | |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 69 | rc = arch_flash_erase_chip(bl); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 70 | if(rc) { |
| 71 | fprintf(stderr, "Error %d erasing chip\n", rc); |
| 72 | return(rc); |
| 73 | } |
| 74 | |
| 75 | printf("done !\n"); |
| 76 | return(rc); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg) |
| 81 | { |
| 82 | GDBusProxy *proxy; |
| 83 | GError *error; |
| 84 | GVariant *parm = NULL; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 85 | error = NULL; |
| 86 | proxy = g_dbus_proxy_new_sync(connection, |
| 87 | G_DBUS_PROXY_FLAGS_NONE, |
| 88 | NULL, /* GDBusInterfaceInfo* */ |
| 89 | "org.openbmc.control.Flash", /* name */ |
| 90 | obj_path, /* object path */ |
| 91 | "org.openbmc.Flash", /* interface name */ |
| 92 | NULL, /* GCancellable */ |
| 93 | &error); |
| 94 | g_assert_no_error(error); |
| 95 | |
| 96 | error = NULL; |
| 97 | if(strcmp(method,"error")==0) { |
| 98 | parm = g_variant_new("(s)",error_msg); |
| 99 | } |
Brad Bishop | 0c82c60 | 2016-04-13 13:36:49 -0400 | [diff] [blame] | 100 | g_dbus_proxy_call_sync(proxy, |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 101 | method, |
| 102 | parm, |
| 103 | G_DBUS_CALL_FLAGS_NONE, |
| 104 | -1, |
| 105 | NULL, |
| 106 | &error); |
| 107 | |
| 108 | g_assert_no_error(error); |
| 109 | } |
| 110 | |
| 111 | static int |
| 112 | program_file(FlashControl* flash_control, const char *file, uint32_t start, uint32_t size) |
| 113 | { |
| 114 | int fd, rc; |
| 115 | ssize_t len; |
| 116 | uint32_t actual_size = 0; |
| 117 | |
| 118 | fd = open(file, O_RDONLY); |
| 119 | if(fd == -1) { |
| 120 | perror("Failed to open file"); |
| 121 | return(fd); |
| 122 | } |
| 123 | printf("About to program \"%s\" at 0x%08x..0x%08x !\n", |
| 124 | file, start, size); |
| 125 | |
| 126 | printf("Programming & Verifying...\n"); |
| 127 | //progress_init(size >> 8); |
| 128 | unsigned int save_size = size; |
| 129 | uint8_t last_progress = 0; |
| 130 | while(size) { |
| 131 | len = read(fd, file_buf, FILE_BUF_SIZE); |
| 132 | if(len < 0) { |
| 133 | perror("Error reading file"); |
| 134 | return(1); |
| 135 | } |
| 136 | if(len == 0) |
| 137 | break; |
| 138 | if(len > size) |
| 139 | len = size; |
| 140 | size -= len; |
| 141 | actual_size += len; |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 142 | rc = blocklevel_write(bl, start, file_buf, len); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 143 | if(rc) { |
| 144 | if(rc == FLASH_ERR_VERIFY_FAILURE) |
| 145 | fprintf(stderr, "Verification failed for" |
| 146 | " chunk at 0x%08x\n", start); |
| 147 | else |
| 148 | fprintf(stderr, "Flash write error %d for" |
| 149 | " chunk at 0x%08x\n", rc, start); |
| 150 | return(rc); |
| 151 | } |
| 152 | start += len; |
| 153 | unsigned int percent = (100*actual_size/save_size); |
| 154 | uint8_t progress = (uint8_t)(percent); |
| 155 | if(progress != last_progress) { |
| 156 | flash_control_emit_progress(flash_control,file,progress); |
| 157 | last_progress = progress; |
| 158 | } |
| 159 | } |
| 160 | close(fd); |
| 161 | |
| 162 | /* If this is a flash partition, adjust its size */ |
| 163 | if(ffsh && ffs_index >= 0) { |
| 164 | printf("Updating actual size in partition header...\n"); |
| 165 | ffs_update_act_size(ffsh, ffs_index, actual_size); |
| 166 | } |
| 167 | return(0); |
| 168 | } |
| 169 | |
| 170 | static void |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 171 | flash_access_cleanup_bmc(void) |
| 172 | { |
| 173 | if(ffsh) |
| 174 | ffs_close(ffsh); |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 175 | arch_flash_close(bl, NULL); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static int |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 179 | flash_access_setup_bmc(bool need_write) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 180 | { |
| 181 | int rc; |
| 182 | printf("Setting up BMC flash\n"); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 183 | |
Joel Stanley | 9d61237 | 2016-10-31 17:27:23 +1030 | [diff] [blame] | 184 | if(arch_flash_bmc(bl, BMC_MTD) != BMC_MTD) { |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 185 | fprintf(stderr, "Failed to init flash chip\n"); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 186 | return FLASH_SETUP_ERROR; |
| 187 | } |
| 188 | |
| 189 | /* Setup cleanup function */ |
| 190 | atexit(flash_access_cleanup_bmc); |
| 191 | return FLASH_OK; |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | flash_access_cleanup_pnor(void) |
| 196 | { |
| 197 | /* Re-lock flash */ |
| 198 | if(need_relock) |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 199 | arch_flash_set_wrprotect(bl, 1); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 200 | |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 201 | flash_access_cleanup_bmc(); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 205 | flash_access_setup_pnor(bool need_write) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 206 | { |
| 207 | int rc; |
| 208 | printf("Setting up BIOS flash\n"); |
| 209 | |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 210 | /* Create the AST flash controller */ |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 211 | |
| 212 | /* Open flash chip */ |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 213 | rc = arch_flash_init(&bl, NULL, true); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 214 | if(rc) { |
| 215 | fprintf(stderr, "Failed to open flash chip\n"); |
| 216 | return FLASH_SETUP_ERROR; |
| 217 | } |
| 218 | |
| 219 | /* Unlock flash (PNOR only) */ |
| 220 | if(need_write) |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 221 | need_relock = arch_flash_set_wrprotect(bl, 0); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 222 | |
| 223 | /* Setup cleanup function */ |
| 224 | atexit(flash_access_cleanup_pnor); |
| 225 | return FLASH_OK; |
| 226 | } |
| 227 | |
| 228 | uint8_t |
| 229 | flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path) |
| 230 | { |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 231 | bool erase = true, program = true; |
| 232 | |
| 233 | int rc; |
| 234 | printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 235 | |
| 236 | /* Prepare for access */ |
| 237 | if(bmc_flash) { |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 238 | rc = flash_access_setup_bmc(erase || program); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 239 | if(rc) { |
| 240 | return FLASH_SETUP_ERROR; |
| 241 | } |
| 242 | } else { |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 243 | rc = flash_access_setup_pnor(erase || program); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 244 | if(rc) { |
| 245 | return FLASH_SETUP_ERROR; |
| 246 | } |
| 247 | } |
| 248 | |
Brad Bishop | 275524e | 2016-09-08 09:09:55 -0400 | [diff] [blame] | 249 | rc = blocklevel_get_info(bl, &fl_name, |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 250 | &fl_total_size, &fl_erase_granule); |
| 251 | if(rc) { |
| 252 | fprintf(stderr, "Error %d getting flash info\n", rc); |
| 253 | return FLASH_SETUP_ERROR; |
| 254 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 255 | if(strcmp(write_file,"")!=0) |
| 256 | { |
| 257 | // If file specified but not size, get size from file |
| 258 | struct stat stbuf; |
| 259 | if(stat(write_file, &stbuf)) { |
| 260 | perror("Failed to get file size"); |
| 261 | return FLASH_ERROR; |
| 262 | } |
| 263 | uint32_t write_size = stbuf.st_size; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 264 | rc = erase_chip(); |
| 265 | if(rc) { |
| 266 | return FLASH_ERROR; |
| 267 | } |
| 268 | rc = program_file(flash_control, write_file, address, write_size); |
| 269 | if(rc) { |
| 270 | return FLASH_ERROR; |
| 271 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 272 | |
| 273 | printf("Flash done\n"); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | printf("Flash tuned\n"); |
| 278 | } |
| 279 | return FLASH_OK; |
| 280 | } |
| 281 | |
| 282 | static void |
| 283 | on_bus_acquired(GDBusConnection *connection, |
| 284 | const gchar *name, |
| 285 | gpointer user_data) |
| 286 | { |
| 287 | cmdline *cmd = user_data; |
| 288 | if(cmd->argc < 4) |
| 289 | { |
| 290 | g_print("flasher [flash name] [filename] [source object]\n"); |
| 291 | g_main_loop_quit(cmd->loop); |
| 292 | return; |
| 293 | } |
| 294 | printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]); |
| 295 | ObjectSkeleton *object; |
| 296 | manager = g_dbus_object_manager_server_new(dbus_object_path); |
| 297 | gchar *s; |
| 298 | s = g_strdup_printf("%s/%s",dbus_object_path,cmd->argv[1]); |
| 299 | |
| 300 | object = object_skeleton_new(s); |
| 301 | g_free(s); |
| 302 | |
| 303 | FlashControl* flash_control = flash_control_skeleton_new(); |
| 304 | object_skeleton_set_flash_control(object, flash_control); |
| 305 | g_object_unref(flash_control); |
| 306 | |
| 307 | /* Export the object (@manager takes its own reference to @object) */ |
| 308 | g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object)); |
| 309 | g_object_unref(object); |
| 310 | |
| 311 | /* Export all objects */ |
| 312 | g_dbus_object_manager_server_set_connection(manager, connection); |
| 313 | bool bmc_flash = false; |
| 314 | uint32_t address = 0; |
| 315 | if(strcmp(cmd->argv[1],"bmc")==0) { |
| 316 | bmc_flash = true; |
| 317 | } |
| 318 | if(strcmp(cmd->argv[1],"bmc_ramdisk")==0) { |
| 319 | bmc_flash = true; |
| 320 | address = 0x20300000; |
| 321 | } |
| 322 | if(strcmp(cmd->argv[1],"bmc_kernel")==0) { |
| 323 | bmc_flash = true; |
| 324 | address = 0x20080000; |
| 325 | } |
| 326 | |
| 327 | int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]); |
| 328 | if(rc) { |
| 329 | flash_message(connection,cmd->argv[3],"error","Flash Error"); |
| 330 | } else { |
| 331 | flash_message(connection,cmd->argv[3],"done",""); |
| 332 | } |
| 333 | |
| 334 | //Object exits when done flashing |
| 335 | g_main_loop_quit(cmd->loop); |
| 336 | } |
| 337 | |
| 338 | int |
| 339 | main(int argc, char *argv[]) |
| 340 | { |
| 341 | GMainLoop *loop; |
| 342 | cmdline cmd; |
| 343 | cmd.argc = argc; |
| 344 | cmd.argv = argv; |
| 345 | |
| 346 | guint id; |
| 347 | loop = g_main_loop_new(NULL, FALSE); |
| 348 | cmd.loop = loop; |
| 349 | |
| 350 | id = g_bus_own_name(DBUS_TYPE, |
| 351 | dbus_name, |
| 352 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 353 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 354 | on_bus_acquired, |
| 355 | NULL, |
| 356 | NULL, |
| 357 | &cmd, |
| 358 | NULL); |
| 359 | |
| 360 | g_main_loop_run(loop); |
| 361 | |
| 362 | g_bus_unown_name(id); |
| 363 | g_main_loop_unref(loop); |
| 364 | |
| 365 | return 0; |
| 366 | } |