blob: b172d3a9cc4c122ca479e345b2874dfcf2225eaa [file] [log] [blame]
Norman Jamesc0d94e02015-10-13 10:06:12 -05001#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
26static const gchar* dbus_object_path = "/org/openbmc/control";
27static const gchar* dbus_object_name = "Flasher_0";
28static const gchar* dbus_name = "org.openbmc.control.Flasher";
29
30static GDBusObjectManagerServer *manager = NULL;
31
32#define __aligned(x) __attribute__((aligned(x)))
33
34#define PFLASH_VERSION "0.8.6"
35
36static bool must_confirm = false;
37static bool dummy_run;
38static bool need_relock;
39static bool bmc_flash;
40#ifdef __powerpc__
41static bool using_sfc;
42#endif
43
44#define FILE_BUF_SIZE 0x10000
45static uint8_t file_buf[FILE_BUF_SIZE] __aligned(0x1000);
46
47static struct spi_flash_ctrl *fl_ctrl;
48static struct flash_chip *fl_chip;
49static struct ffs_handle *ffsh;
50static uint32_t fl_total_size, fl_erase_granule;
51static const char *fl_name;
52static int32_t ffs_index = -1;
53
Norman James166acf42015-10-22 07:11:51 -050054static uint8_t FLASH_OK = 0;
Norman James05028c92015-11-01 19:14:25 -060055static uint8_t FLASH_ERROR = 0x01;
56static uint8_t FLASH_SETUP_ERROR = 0x02;
Norman James7b1afb62015-10-28 20:28:37 -050057static struct blocklevel_device *bl;
Norman Jamesc0d94e02015-10-13 10:06:12 -050058
59static int erase_chip(void)
60{
61 int rc = 0;
62
63 printf("Erasing... (may take a while !) ");
64 fflush(stdout);
65
66 rc = flash_erase_chip(fl_chip);
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
Norman James7b1afb62015-10-28 20:28:37 -050076
Norman James166acf42015-10-22 07:11:51 -050077void flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg)
78{
79 GDBusProxy *proxy;
80 GError *error;
81 GVariant *parm = NULL;
82 GVariant *result;
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 }
98 result = g_dbus_proxy_call_sync (proxy,
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
Norman Jamesc0d94e02015-10-13 10:06:12 -0500110static int 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;
140 rc = flash_write(fl_chip, start, file_buf, len, true);
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
168static void do_read_file(const char *file, uint32_t start, uint32_t size)
169{
170 int fd, rc;
171 ssize_t len;
172 uint32_t done = 0;
173
174 fd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 00666);
175 if (fd == -1) {
176 perror("Failed to open file");
177 exit(1);
178 }
179 printf("Reading to \"%s\" from 0x%08x..0x%08x !\n",
180 file, start, size);
181
182 progress_init(size >> 8);
183 while(size) {
184 len = size > FILE_BUF_SIZE ? FILE_BUF_SIZE : size;
185 rc = flash_read(fl_chip, start, file_buf, len);
186 if (rc) {
187 fprintf(stderr, "Flash read error %d for"
188 " chunk at 0x%08x\n", rc, start);
189 exit(1);
190 }
191 rc = write(fd, file_buf, len);
192 if (rc < 0) {
193 perror("Error writing file");
194 exit(1);
195 }
196 start += len;
197 size -= len;
198 done += len;
199 progress_tick(done >> 8);
200 }
201 progress_end();
202 close(fd);
203}
204static void flash_access_cleanup_bmc(void)
205{
206 if (ffsh)
207 ffs_close(ffsh);
208 flash_exit(fl_chip);
209 ast_sf_close(fl_ctrl);
210 close_devs();
211}
212
Norman James05028c92015-11-01 19:14:25 -0600213static int flash_access_setup_bmc(bool use_lpc, bool need_write)
Norman Jamesc0d94e02015-10-13 10:06:12 -0500214{
215 int rc;
Norman James166acf42015-10-22 07:11:51 -0500216 printf("Setting up BMC flash\n");
Norman Jamesc0d94e02015-10-13 10:06:12 -0500217 /* Open and map devices */
218 open_devs(use_lpc, true);
219
220 /* Create the AST flash controller */
221 rc = ast_sf_open(AST_SF_TYPE_BMC, &fl_ctrl);
222 if (rc) {
223 fprintf(stderr, "Failed to open controller\n");
Norman James05028c92015-11-01 19:14:25 -0600224 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500225 }
226
227 /* Open flash chip */
228 rc = flash_init(fl_ctrl, &fl_chip);
229 if (rc) {
230 fprintf(stderr, "Failed to open flash chip\n");
Norman James05028c92015-11-01 19:14:25 -0600231 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500232 }
233
234 /* Setup cleanup function */
235 atexit(flash_access_cleanup_bmc);
Norman James05028c92015-11-01 19:14:25 -0600236 return FLASH_OK;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500237}
238
239static void flash_access_cleanup_pnor(void)
240{
241 /* Re-lock flash */
242 if (need_relock)
243 set_wrprotect(true);
244
245 if (ffsh)
246 ffs_close(ffsh);
247 flash_exit(fl_chip);
248#ifdef __powerpc__
249 if (using_sfc)
250 sfc_close(fl_ctrl);
251 else
252 ast_sf_close(fl_ctrl);
253#else
254 ast_sf_close(fl_ctrl);
255#endif
256 close_devs();
257}
258
Norman James05028c92015-11-01 19:14:25 -0600259static int flash_access_setup_pnor(bool use_lpc, bool use_sfc, bool need_write)
Norman Jamesc0d94e02015-10-13 10:06:12 -0500260{
261 int rc;
Norman James166acf42015-10-22 07:11:51 -0500262 printf("Setting up BIOS flash\n");
Norman Jamesc0d94e02015-10-13 10:06:12 -0500263
264 /* Open and map devices */
265 open_devs(use_lpc, false);
266
267#ifdef __powerpc__
268 if (use_sfc) {
269 /* Create the SFC flash controller */
270 rc = sfc_open(&fl_ctrl);
271 if (rc) {
272 fprintf(stderr, "Failed to open controller\n");
Norman James05028c92015-11-01 19:14:25 -0600273 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500274 }
275 using_sfc = true;
276 } else {
277#endif
278 /* Create the AST flash controller */
279 rc = ast_sf_open(AST_SF_TYPE_PNOR, &fl_ctrl);
280 if (rc) {
281 fprintf(stderr, "Failed to open controller\n");
Norman James05028c92015-11-01 19:14:25 -0600282 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500283 }
284#ifdef __powerpc__
285 }
286#endif
287
288 /* Open flash chip */
289 rc = flash_init(fl_ctrl, &fl_chip);
290 if (rc) {
291 fprintf(stderr, "Failed to open flash chip\n");
Norman James05028c92015-11-01 19:14:25 -0600292 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500293 }
294
295 /* Unlock flash (PNOR only) */
296 if (need_write)
297 need_relock = set_wrprotect(false);
298
299 /* Setup cleanup function */
300 atexit(flash_access_cleanup_pnor);
Norman James05028c92015-11-01 19:14:25 -0600301 return FLASH_OK;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500302}
303
Norman James15aaa802015-10-28 06:54:48 -0500304uint8_t flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
Norman Jamesc0d94e02015-10-13 10:06:12 -0500305{
306 bool has_sfc = false, has_ast = false, use_lpc = true;
307 bool erase = true, program = true;
Norman James15aaa802015-10-28 06:54:48 -0500308
Norman Jamesc0d94e02015-10-13 10:06:12 -0500309 int rc;
Norman James15aaa802015-10-28 06:54:48 -0500310 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
Norman Jamesc0d94e02015-10-13 10:06:12 -0500311#ifdef __arm__
312 /* Check platform */
313 check_platform(&has_sfc, &has_ast);
314
315 /* Prepare for access */
316 if (bmc_flash) {
317 if (!has_ast) {
318 fprintf(stderr, "No BMC on this platform\n");
Norman James05028c92015-11-01 19:14:25 -0600319 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500320 }
Norman James05028c92015-11-01 19:14:25 -0600321 rc = flash_access_setup_bmc(use_lpc, erase || program);
322 if (rc) {
323 return FLASH_SETUP_ERROR;
324 }
Norman Jamesc0d94e02015-10-13 10:06:12 -0500325 } else {
326 if (!has_ast && !has_sfc) {
327 fprintf(stderr, "No BMC nor SFC on this platform\n");
Norman James05028c92015-11-01 19:14:25 -0600328 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500329 }
Norman James05028c92015-11-01 19:14:25 -0600330 rc = flash_access_setup_pnor(use_lpc, has_sfc, erase || program);
331 if (rc) {
332 return FLASH_SETUP_ERROR;
333 }
Norman Jamesc0d94e02015-10-13 10:06:12 -0500334 }
335
336 rc = flash_get_info(fl_chip, &fl_name,
337 &fl_total_size, &fl_erase_granule);
338 if (rc) {
339 fprintf(stderr, "Error %d getting flash info\n", rc);
Norman James05028c92015-11-01 19:14:25 -0600340 return FLASH_SETUP_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500341 }
342#endif
343 if (strcmp(write_file,"")!=0)
344 {
345 // If file specified but not size, get size from file
346 struct stat stbuf;
347 if (stat(write_file, &stbuf)) {
348 perror("Failed to get file size");
Norman James166acf42015-10-22 07:11:51 -0500349 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500350 }
351 uint32_t write_size = stbuf.st_size;
352#ifdef __arm__
353 rc = erase_chip();
354 if (rc) {
Norman James166acf42015-10-22 07:11:51 -0500355 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500356 }
357 rc = program_file(flash_control, write_file, address, write_size);
358 if (rc) {
Norman James166acf42015-10-22 07:11:51 -0500359 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500360 }
361#endif
362
Norman Jamesc0d94e02015-10-13 10:06:12 -0500363 printf("Flash done\n");
364 }
365 else
366 {
Norman Jamesc0d94e02015-10-13 10:06:12 -0500367 printf("Flash tuned\n");
Norman James05028c92015-11-01 19:14:25 -0600368 //tune twice
369 if (!bmc_flash) {
370 flash_access_setup_pnor(use_lpc, has_sfc, erase || program);
371 }
Norman Jamesc0d94e02015-10-13 10:06:12 -0500372 }
Norman James166acf42015-10-22 07:11:51 -0500373 return FLASH_OK;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500374}
375
376static void
377on_bus_acquired (GDBusConnection *connection,
378 const gchar *name,
379 gpointer user_data)
380{
381
382 cmdline *cmd = user_data;
Norman James166acf42015-10-22 07:11:51 -0500383 if (cmd->argc < 4)
Norman Jamesc0d94e02015-10-13 10:06:12 -0500384 {
Norman James166acf42015-10-22 07:11:51 -0500385 g_print("flasher [flash name] [filename] [source object]\n");
Norman Jamesbb145c92015-11-01 07:37:47 -0600386 g_main_loop_quit(cmd->loop);
Norman Jamesc0d94e02015-10-13 10:06:12 -0500387 return;
388 }
Norman James166acf42015-10-22 07:11:51 -0500389 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
Norman Jamesc0d94e02015-10-13 10:06:12 -0500390 ObjectSkeleton *object;
391 manager = g_dbus_object_manager_server_new (dbus_object_path);
392 gchar *s;
393 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[1]);
394
395 object = object_skeleton_new (s);
396 g_free (s);
397
398 FlashControl* flash_control = flash_control_skeleton_new ();
399 object_skeleton_set_flash_control (object, flash_control);
400 g_object_unref (flash_control);
401
402 /* Export the object (@manager takes its own reference to @object) */
403 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
404 g_object_unref (object);
405
406 /* Export all objects */
407 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James166acf42015-10-22 07:11:51 -0500408 bool bmc_flash = false;
Norman James15aaa802015-10-28 06:54:48 -0500409 uint32_t address = 0;
Norman James166acf42015-10-22 07:11:51 -0500410 if (strcmp(cmd->argv[1],"bmc")==0) {
411 bmc_flash = true;
412 }
Norman James15aaa802015-10-28 06:54:48 -0500413 if (strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
414 bmc_flash = true;
415 address = 0x20300000;
416 }
417 if (strcmp(cmd->argv[1],"bmc_kernel")==0) {
418 bmc_flash = true;
419 address = 0x20080000;
420 }
421
422 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
Norman James05028c92015-11-01 19:14:25 -0600423 if (rc) {
Norman James166acf42015-10-22 07:11:51 -0500424 flash_message(connection,cmd->argv[3],"error","Flash Error");
425 } else {
426 flash_message(connection,cmd->argv[3],"done","");
427 }
Norman Jamesc0d94e02015-10-13 10:06:12 -0500428
429 //Object exits when done flashing
430 g_main_loop_quit(cmd->loop);
431}
432
433int main(int argc, char *argv[])
434{
435
436 GMainLoop *loop;
437 cmdline cmd;
438 cmd.argc = argc;
439 cmd.argv = argv;
440
441 guint id;
442 loop = g_main_loop_new (NULL, FALSE);
443 cmd.loop = loop;
444
445 id = g_bus_own_name (DBUS_TYPE,
446 dbus_name,
447 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
448 G_BUS_NAME_OWNER_FLAGS_REPLACE,
449 on_bus_acquired,
450 NULL,
451 NULL,
452 &cmd,
453 NULL);
454
455 g_main_loop_run (loop);
456
457 g_bus_unown_name (id);
458 g_main_loop_unref (loop);
459
460 return 0;
461}