Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <stdlib.h>
|
| 3 | #include <string.h>
|
| 4 | #include <fcntl.h>
|
| 5 | #include <sys/mman.h>
|
| 6 | #include <sys/types.h>
|
| 7 | #include <sys/stat.h>
|
| 8 | #include <unistd.h>
|
| 9 | #include <byteswap.h>
|
| 10 | #include <stdint.h>
|
| 11 | #include <stdbool.h>
|
| 12 | #include <getopt.h>
|
| 13 | #include <limits.h>
|
| 14 | #include <arpa/inet.h>
|
| 15 | #include <assert.h>
|
| 16 |
|
| 17 | #include <libflash/libflash.h>
|
| 18 | #include <libflash/libffs.h>
|
| 19 | #include "progress.h"
|
| 20 | #include "io.h"
|
| 21 | #include "ast.h"
|
| 22 | #include "sfc-ctrl.h"
|
| 23 | #include "interfaces/openbmc_intf.h"
|
| 24 | #include "includes/openbmc.h"
|
| 25 |
|
| 26 | static const gchar* dbus_object_path = "/org/openbmc/control";
|
| 27 | static const gchar* dbus_object_name = "Flasher_0";
|
| 28 | static const gchar* dbus_name = "org.openbmc.control.Flasher";
|
| 29 |
|
| 30 | static GDBusObjectManagerServer *manager = NULL;
|
| 31 |
|
| 32 | #define __aligned(x) __attribute__((aligned(x)))
|
| 33 |
|
| 34 | #define PFLASH_VERSION "0.8.6"
|
| 35 |
|
| 36 | static bool must_confirm = false;
|
| 37 | static bool dummy_run;
|
| 38 | static bool need_relock;
|
| 39 | static bool bmc_flash;
|
| 40 | #ifdef __powerpc__
|
| 41 | static bool using_sfc;
|
| 42 | #endif
|
| 43 |
|
| 44 | #define FILE_BUF_SIZE 0x10000
|
| 45 | static uint8_t file_buf[FILE_BUF_SIZE] __aligned(0x1000);
|
| 46 |
|
| 47 | static struct spi_flash_ctrl *fl_ctrl;
|
| 48 | static struct flash_chip *fl_chip;
|
| 49 | static struct ffs_handle *ffsh;
|
| 50 | static uint32_t fl_total_size, fl_erase_granule;
|
| 51 | static const char *fl_name;
|
| 52 | static int32_t ffs_index = -1;
|
| 53 |
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 54 | static uint8_t FLASH_OK = 0;
|
| 55 | static uint8_t FLASH_ERROR = 1;
|
Norman James | 7b1afb6 | 2015-10-28 20:28:37 -0500 | [diff] [blame] | 56 | static struct blocklevel_device *bl;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 57 |
|
| 58 | static int erase_chip(void)
|
| 59 | {
|
| 60 | int rc = 0;
|
| 61 |
|
| 62 | printf("Erasing... (may take a while !) ");
|
| 63 | fflush(stdout);
|
| 64 |
|
| 65 | rc = flash_erase_chip(fl_chip);
|
| 66 | if (rc) {
|
| 67 | fprintf(stderr, "Error %d erasing chip\n", rc);
|
| 68 | return(rc);
|
| 69 | }
|
| 70 |
|
| 71 | printf("done !\n");
|
| 72 | return (rc);
|
| 73 | }
|
| 74 |
|
Norman James | 7b1afb6 | 2015-10-28 20:28:37 -0500 | [diff] [blame] | 75 |
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 76 | void flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg)
|
| 77 | {
|
| 78 | GDBusProxy *proxy;
|
| 79 | GError *error;
|
| 80 | GVariant *parm = NULL;
|
| 81 | GVariant *result;
|
| 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 | }
|
| 97 | result = g_dbus_proxy_call_sync (proxy,
|
| 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 |
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 109 | static int 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;
|
| 139 | rc = flash_write(fl_chip, start, file_buf, len, true);
|
| 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 |
|
| 159 | /* If this is a flash partition, adjust its size */
|
| 160 | if (ffsh && ffs_index >= 0) {
|
| 161 | printf("Updating actual size in partition header...\n");
|
| 162 | ffs_update_act_size(ffsh, ffs_index, actual_size);
|
| 163 | }
|
| 164 | return(0);
|
| 165 | }
|
| 166 |
|
| 167 | static void do_read_file(const char *file, uint32_t start, uint32_t size)
|
| 168 | {
|
| 169 | int fd, rc;
|
| 170 | ssize_t len;
|
| 171 | uint32_t done = 0;
|
| 172 |
|
| 173 | fd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 00666);
|
| 174 | if (fd == -1) {
|
| 175 | perror("Failed to open file");
|
| 176 | exit(1);
|
| 177 | }
|
| 178 | printf("Reading to \"%s\" from 0x%08x..0x%08x !\n",
|
| 179 | file, start, size);
|
| 180 |
|
| 181 | progress_init(size >> 8);
|
| 182 | while(size) {
|
| 183 | len = size > FILE_BUF_SIZE ? FILE_BUF_SIZE : size;
|
| 184 | rc = flash_read(fl_chip, start, file_buf, len);
|
| 185 | if (rc) {
|
| 186 | fprintf(stderr, "Flash read error %d for"
|
| 187 | " chunk at 0x%08x\n", rc, start);
|
| 188 | exit(1);
|
| 189 | }
|
| 190 | rc = write(fd, file_buf, len);
|
| 191 | if (rc < 0) {
|
| 192 | perror("Error writing file");
|
| 193 | exit(1);
|
| 194 | }
|
| 195 | start += len;
|
| 196 | size -= len;
|
| 197 | done += len;
|
| 198 | progress_tick(done >> 8);
|
| 199 | }
|
| 200 | progress_end();
|
| 201 | close(fd);
|
| 202 | }
|
| 203 | static void flash_access_cleanup_bmc(void)
|
| 204 | {
|
| 205 | if (ffsh)
|
| 206 | ffs_close(ffsh);
|
| 207 | flash_exit(fl_chip);
|
| 208 | ast_sf_close(fl_ctrl);
|
| 209 | close_devs();
|
| 210 | }
|
| 211 |
|
| 212 | static void flash_access_setup_bmc(bool use_lpc, bool need_write)
|
| 213 | {
|
| 214 | int rc;
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 215 | printf("Setting up BMC flash\n");
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 216 | /* Open and map devices */
|
| 217 | open_devs(use_lpc, true);
|
| 218 |
|
| 219 | /* Create the AST flash controller */
|
| 220 | rc = ast_sf_open(AST_SF_TYPE_BMC, &fl_ctrl);
|
| 221 | if (rc) {
|
| 222 | fprintf(stderr, "Failed to open controller\n");
|
| 223 | exit(1);
|
| 224 | }
|
| 225 |
|
| 226 | /* Open flash chip */
|
| 227 | rc = flash_init(fl_ctrl, &fl_chip);
|
| 228 | if (rc) {
|
| 229 | fprintf(stderr, "Failed to open flash chip\n");
|
| 230 | exit(1);
|
| 231 | }
|
| 232 |
|
| 233 | /* Setup cleanup function */
|
| 234 | atexit(flash_access_cleanup_bmc);
|
| 235 | }
|
| 236 |
|
| 237 | static void flash_access_cleanup_pnor(void)
|
| 238 | {
|
| 239 | /* Re-lock flash */
|
| 240 | if (need_relock)
|
| 241 | set_wrprotect(true);
|
| 242 |
|
| 243 | if (ffsh)
|
| 244 | ffs_close(ffsh);
|
| 245 | flash_exit(fl_chip);
|
| 246 | #ifdef __powerpc__
|
| 247 | if (using_sfc)
|
| 248 | sfc_close(fl_ctrl);
|
| 249 | else
|
| 250 | ast_sf_close(fl_ctrl);
|
| 251 | #else
|
| 252 | ast_sf_close(fl_ctrl);
|
| 253 | #endif
|
| 254 | close_devs();
|
| 255 | }
|
| 256 |
|
| 257 | static void flash_access_setup_pnor(bool use_lpc, bool use_sfc, bool need_write)
|
| 258 | {
|
| 259 | int rc;
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 260 | printf("Setting up BIOS flash\n");
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 261 |
|
| 262 | /* Open and map devices */
|
| 263 | open_devs(use_lpc, false);
|
| 264 |
|
| 265 | #ifdef __powerpc__
|
| 266 | if (use_sfc) {
|
| 267 | /* Create the SFC flash controller */
|
| 268 | rc = sfc_open(&fl_ctrl);
|
| 269 | if (rc) {
|
| 270 | fprintf(stderr, "Failed to open controller\n");
|
| 271 | exit(1);
|
| 272 | }
|
| 273 | using_sfc = true;
|
| 274 | } else {
|
| 275 | #endif
|
| 276 | /* Create the AST flash controller */
|
| 277 | rc = ast_sf_open(AST_SF_TYPE_PNOR, &fl_ctrl);
|
| 278 | if (rc) {
|
| 279 | fprintf(stderr, "Failed to open controller\n");
|
| 280 | exit(1);
|
| 281 | }
|
| 282 | #ifdef __powerpc__
|
| 283 | }
|
| 284 | #endif
|
| 285 |
|
| 286 | /* Open flash chip */
|
| 287 | rc = flash_init(fl_ctrl, &fl_chip);
|
| 288 | if (rc) {
|
| 289 | fprintf(stderr, "Failed to open flash chip\n");
|
| 290 | exit(1);
|
| 291 | }
|
| 292 |
|
| 293 | /* Unlock flash (PNOR only) */
|
| 294 | if (need_write)
|
| 295 | need_relock = set_wrprotect(false);
|
| 296 |
|
| 297 | /* Setup cleanup function */
|
| 298 | atexit(flash_access_cleanup_pnor);
|
| 299 | }
|
| 300 |
|
Norman James | 15aaa80 | 2015-10-28 06:54:48 -0500 | [diff] [blame] | 301 | uint8_t flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 302 | {
|
| 303 | bool has_sfc = false, has_ast = false, use_lpc = true;
|
| 304 | bool erase = true, program = true;
|
Norman James | 15aaa80 | 2015-10-28 06:54:48 -0500 | [diff] [blame] | 305 |
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 306 | int rc;
|
Norman James | 15aaa80 | 2015-10-28 06:54:48 -0500 | [diff] [blame] | 307 | printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 308 | #ifdef __arm__
|
| 309 | /* Check platform */
|
| 310 | check_platform(&has_sfc, &has_ast);
|
| 311 |
|
| 312 | /* Prepare for access */
|
| 313 | if (bmc_flash) {
|
| 314 | if (!has_ast) {
|
| 315 | fprintf(stderr, "No BMC on this platform\n");
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 316 | return FLASH_ERROR;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 317 | }
|
| 318 | flash_access_setup_bmc(use_lpc, erase || program);
|
| 319 | } else {
|
| 320 | if (!has_ast && !has_sfc) {
|
| 321 | fprintf(stderr, "No BMC nor SFC on this platform\n");
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 322 | return FLASH_ERROR;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 323 | }
|
| 324 | flash_access_setup_pnor(use_lpc, has_sfc, erase || program);
|
| 325 | }
|
| 326 |
|
| 327 | rc = flash_get_info(fl_chip, &fl_name,
|
| 328 | &fl_total_size, &fl_erase_granule);
|
| 329 | if (rc) {
|
| 330 | fprintf(stderr, "Error %d getting flash info\n", rc);
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 331 | return FLASH_ERROR;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 332 | }
|
| 333 | #endif
|
| 334 | if (strcmp(write_file,"")!=0)
|
| 335 | {
|
| 336 | // If file specified but not size, get size from file
|
| 337 | struct stat stbuf;
|
| 338 | if (stat(write_file, &stbuf)) {
|
| 339 | perror("Failed to get file size");
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 340 | return FLASH_ERROR;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 341 | }
|
| 342 | uint32_t write_size = stbuf.st_size;
|
| 343 | #ifdef __arm__
|
| 344 | rc = erase_chip();
|
| 345 | if (rc) {
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 346 | return FLASH_ERROR;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 347 | }
|
| 348 | rc = program_file(flash_control, write_file, address, write_size);
|
| 349 | if (rc) {
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 350 | return FLASH_ERROR;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 351 | }
|
| 352 | #endif
|
| 353 |
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 354 | printf("Flash done\n");
|
| 355 | }
|
| 356 | else
|
| 357 | {
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 358 | printf("Flash tuned\n");
|
| 359 | }
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 360 | return FLASH_OK;
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 361 | }
|
| 362 |
|
| 363 | static void
|
| 364 | on_bus_acquired (GDBusConnection *connection,
|
| 365 | const gchar *name,
|
| 366 | gpointer user_data)
|
| 367 | {
|
| 368 |
|
| 369 | cmdline *cmd = user_data;
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 370 | if (cmd->argc < 4)
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 371 | {
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 372 | g_print("flasher [flash name] [filename] [source object]\n");
|
Norman James | bb145c9 | 2015-11-01 07:37:47 -0600 | [diff] [blame] | 373 | g_main_loop_quit(cmd->loop);
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 374 | return;
|
| 375 | }
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 376 | printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 377 | ObjectSkeleton *object;
|
| 378 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
| 379 | gchar *s;
|
| 380 | s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[1]);
|
| 381 |
|
| 382 | object = object_skeleton_new (s);
|
| 383 | g_free (s);
|
| 384 |
|
| 385 | FlashControl* flash_control = flash_control_skeleton_new ();
|
| 386 | object_skeleton_set_flash_control (object, flash_control);
|
| 387 | g_object_unref (flash_control);
|
| 388 |
|
| 389 | /* Export the object (@manager takes its own reference to @object) */
|
| 390 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 391 | g_object_unref (object);
|
| 392 |
|
| 393 | /* Export all objects */
|
| 394 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 395 | bool bmc_flash = false;
|
Norman James | 15aaa80 | 2015-10-28 06:54:48 -0500 | [diff] [blame] | 396 | uint32_t address = 0;
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 397 | if (strcmp(cmd->argv[1],"bmc")==0) {
|
| 398 | bmc_flash = true;
|
| 399 | }
|
Norman James | 15aaa80 | 2015-10-28 06:54:48 -0500 | [diff] [blame] | 400 | if (strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
|
| 401 | bmc_flash = true;
|
| 402 | address = 0x20300000;
|
| 403 | }
|
| 404 | if (strcmp(cmd->argv[1],"bmc_kernel")==0) {
|
| 405 | bmc_flash = true;
|
| 406 | address = 0x20080000;
|
| 407 | }
|
| 408 |
|
| 409 | int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
|
Norman James | 166acf4 | 2015-10-22 07:11:51 -0500 | [diff] [blame] | 410 | if (rc == FLASH_ERROR) {
|
| 411 | flash_message(connection,cmd->argv[3],"error","Flash Error");
|
| 412 | } else {
|
| 413 | flash_message(connection,cmd->argv[3],"done","");
|
| 414 | }
|
Norman James | c0d94e0 | 2015-10-13 10:06:12 -0500 | [diff] [blame] | 415 |
|
| 416 | //Object exits when done flashing
|
| 417 | g_main_loop_quit(cmd->loop);
|
| 418 | }
|
| 419 |
|
| 420 | int main(int argc, char *argv[])
|
| 421 | {
|
| 422 |
|
| 423 | GMainLoop *loop;
|
| 424 | cmdline cmd;
|
| 425 | cmd.argc = argc;
|
| 426 | cmd.argv = argv;
|
| 427 |
|
| 428 | guint id;
|
| 429 | loop = g_main_loop_new (NULL, FALSE);
|
| 430 | cmd.loop = loop;
|
| 431 |
|
| 432 | id = g_bus_own_name (DBUS_TYPE,
|
| 433 | dbus_name,
|
| 434 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 435 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 436 | on_bus_acquired,
|
| 437 | NULL,
|
| 438 | NULL,
|
| 439 | &cmd,
|
| 440 | NULL);
|
| 441 |
|
| 442 | g_main_loop_run (loop);
|
| 443 |
|
| 444 | g_bus_unown_name (id);
|
| 445 | g_main_loop_unref (loop);
|
| 446 |
|
| 447 | return 0;
|
| 448 | }
|