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