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