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