blob: 2d808476b0926811f5b0ca093d0fd1c96950f7c4 [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#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
54static uint8_t FLASH_OK = 0;
55static uint8_t FLASH_ERROR = 0x01;
56static uint8_t FLASH_SETUP_ERROR = 0x02;
57static struct blocklevel_device *bl;
58
59static int
60erase_chip(void)
61{
62 int rc = 0;
63
64 printf("Erasing... (may take a while !) ");
65 fflush(stdout);
66
67 rc = flash_erase_chip(fl_chip);
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
77void
78flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg)
79{
80 GDBusProxy *proxy;
81 GError *error;
82 GVariant *parm = NULL;
83 GVariant *result;
84 error = NULL;
85 proxy = g_dbus_proxy_new_sync(connection,
86 G_DBUS_PROXY_FLAGS_NONE,
87 NULL, /* GDBusInterfaceInfo* */
88 "org.openbmc.control.Flash", /* name */
89 obj_path, /* object path */
90 "org.openbmc.Flash", /* interface name */
91 NULL, /* GCancellable */
92 &error);
93 g_assert_no_error(error);
94
95 error = NULL;
96 if(strcmp(method,"error")==0) {
97 parm = g_variant_new("(s)",error_msg);
98 }
99 result = g_dbus_proxy_call_sync(proxy,
100 method,
101 parm,
102 G_DBUS_CALL_FLAGS_NONE,
103 -1,
104 NULL,
105 &error);
106
107 g_assert_no_error(error);
108}
109
110static int
111program_file(FlashControl* flash_control, const char *file, uint32_t start, uint32_t size)
112{
113 int fd, rc;
114 ssize_t len;
115 uint32_t actual_size = 0;
116
117 fd = open(file, O_RDONLY);
118 if(fd == -1) {
119 perror("Failed to open file");
120 return(fd);
121 }
122 printf("About to program \"%s\" at 0x%08x..0x%08x !\n",
123 file, start, size);
124
125 printf("Programming & Verifying...\n");
126 //progress_init(size >> 8);
127 unsigned int save_size = size;
128 uint8_t last_progress = 0;
129 while(size) {
130 len = read(fd, file_buf, FILE_BUF_SIZE);
131 if(len < 0) {
132 perror("Error reading file");
133 return(1);
134 }
135 if(len == 0)
136 break;
137 if(len > size)
138 len = size;
139 size -= len;
140 actual_size += len;
141 rc = flash_write(fl_chip, start, file_buf, len, true);
142 if(rc) {
143 if(rc == FLASH_ERR_VERIFY_FAILURE)
144 fprintf(stderr, "Verification failed for"
145 " chunk at 0x%08x\n", start);
146 else
147 fprintf(stderr, "Flash write error %d for"
148 " chunk at 0x%08x\n", rc, start);
149 return(rc);
150 }
151 start += len;
152 unsigned int percent = (100*actual_size/save_size);
153 uint8_t progress = (uint8_t)(percent);
154 if(progress != last_progress) {
155 flash_control_emit_progress(flash_control,file,progress);
156 last_progress = progress;
157 }
158 }
159 close(fd);
160
161 /* If this is a flash partition, adjust its size */
162 if(ffsh && ffs_index >= 0) {
163 printf("Updating actual size in partition header...\n");
164 ffs_update_act_size(ffsh, ffs_index, actual_size);
165 }
166 return(0);
167}
168
169static void
170do_read_file(const char *file, uint32_t start, uint32_t size)
171{
172 int fd, rc;
173 ssize_t len;
174 uint32_t done = 0;
175
176 fd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 00666);
177 if(fd == -1) {
178 perror("Failed to open file");
179 exit(1);
180 }
181 printf("Reading to \"%s\" from 0x%08x..0x%08x !\n",
182 file, start, size);
183
184 progress_init(size >> 8);
185 while(size) {
186 len = size > FILE_BUF_SIZE ? FILE_BUF_SIZE : size;
187 rc = flash_read(fl_chip, start, file_buf, len);
188 if(rc) {
189 fprintf(stderr, "Flash read error %d for"
190 " chunk at 0x%08x\n", rc, start);
191 exit(1);
192 }
193 rc = write(fd, file_buf, len);
194 if(rc < 0) {
195 perror("Error writing file");
196 exit(1);
197 }
198 start += len;
199 size -= len;
200 done += len;
201 progress_tick(done >> 8);
202 }
203 progress_end();
204 close(fd);
205}
206
207static void
208flash_access_cleanup_bmc(void)
209{
210 if(ffsh)
211 ffs_close(ffsh);
212 flash_exit(fl_chip);
213 ast_sf_close(fl_ctrl);
214 close_devs();
215}
216
217static int
218flash_access_setup_bmc(bool use_lpc, bool need_write)
219{
220 int rc;
221 printf("Setting up BMC flash\n");
222 /* Open and map devices */
223 open_devs(use_lpc, true);
224
225 /* Create the AST flash controller */
226 rc = ast_sf_open(AST_SF_TYPE_BMC, &fl_ctrl);
227 if(rc) {
228 fprintf(stderr, "Failed to open controller\n");
229 return FLASH_SETUP_ERROR;
230 }
231
232 /* Open flash chip */
233 rc = flash_init(fl_ctrl, &fl_chip);
234 if(rc) {
235 fprintf(stderr, "Failed to open flash chip\n");
236 return FLASH_SETUP_ERROR;
237 }
238
239 /* Setup cleanup function */
240 atexit(flash_access_cleanup_bmc);
241 return FLASH_OK;
242}
243
244static void
245flash_access_cleanup_pnor(void)
246{
247 /* Re-lock flash */
248 if(need_relock)
249 set_wrprotect(true);
250
251 if(ffsh)
252 ffs_close(ffsh);
253 flash_exit(fl_chip);
254#ifdef __powerpc__
255 if(using_sfc)
256 sfc_close(fl_ctrl);
257 else
258 ast_sf_close(fl_ctrl);
259#else
260 ast_sf_close(fl_ctrl);
261#endif
262 close_devs();
263}
264
265static int
266flash_access_setup_pnor(bool use_lpc, bool use_sfc, bool need_write)
267{
268 int rc;
269 printf("Setting up BIOS flash\n");
270
271 /* Open and map devices */
272 open_devs(use_lpc, false);
273
274#ifdef __powerpc__
275 if(use_sfc) {
276 /* Create the SFC flash controller */
277 rc = sfc_open(&fl_ctrl);
278 if(rc) {
279 fprintf(stderr, "Failed to open controller\n");
280 return FLASH_SETUP_ERROR;
281 }
282 using_sfc = true;
283 } else {
284#endif
285 /* Create the AST flash controller */
286 rc = ast_sf_open(AST_SF_TYPE_PNOR, &fl_ctrl);
287 if(rc) {
288 fprintf(stderr, "Failed to open controller\n");
289 return FLASH_SETUP_ERROR;
290 }
291#ifdef __powerpc__
292 }
293#endif
294
295 /* Open flash chip */
296 rc = flash_init(fl_ctrl, &fl_chip);
297 if(rc) {
298 fprintf(stderr, "Failed to open flash chip\n");
299 return FLASH_SETUP_ERROR;
300 }
301
302 /* Unlock flash (PNOR only) */
303 if(need_write)
304 need_relock = set_wrprotect(false);
305
306 /* Setup cleanup function */
307 atexit(flash_access_cleanup_pnor);
308 return FLASH_OK;
309}
310
311uint8_t
312flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
313{
314 bool has_sfc = false, has_ast = false, use_lpc = true;
315 bool erase = true, program = true;
316
317 int rc;
318 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
319#ifdef __arm__
320 /* Check platform */
321 check_platform(&has_sfc, &has_ast);
322
323 /* Prepare for access */
324 if(bmc_flash) {
325 if(!has_ast) {
326 fprintf(stderr, "No BMC on this platform\n");
327 return FLASH_SETUP_ERROR;
328 }
329 rc = flash_access_setup_bmc(use_lpc, erase || program);
330 if(rc) {
331 return FLASH_SETUP_ERROR;
332 }
333 } else {
334 if(!has_ast && !has_sfc) {
335 fprintf(stderr, "No BMC nor SFC on this platform\n");
336 return FLASH_SETUP_ERROR;
337 }
338 rc = flash_access_setup_pnor(use_lpc, has_sfc, erase || program);
339 if(rc) {
340 return FLASH_SETUP_ERROR;
341 }
342 }
343
344 rc = flash_get_info(fl_chip, &fl_name,
345 &fl_total_size, &fl_erase_granule);
346 if(rc) {
347 fprintf(stderr, "Error %d getting flash info\n", rc);
348 return FLASH_SETUP_ERROR;
349 }
350#endif
351 if(strcmp(write_file,"")!=0)
352 {
353 // If file specified but not size, get size from file
354 struct stat stbuf;
355 if(stat(write_file, &stbuf)) {
356 perror("Failed to get file size");
357 return FLASH_ERROR;
358 }
359 uint32_t write_size = stbuf.st_size;
360#ifdef __arm__
361 rc = erase_chip();
362 if(rc) {
363 return FLASH_ERROR;
364 }
365 rc = program_file(flash_control, write_file, address, write_size);
366 if(rc) {
367 return FLASH_ERROR;
368 }
369#endif
370
371 printf("Flash done\n");
372 }
373 else
374 {
375 printf("Flash tuned\n");
376 }
377 return FLASH_OK;
378}
379
380static void
381on_bus_acquired(GDBusConnection *connection,
382 const gchar *name,
383 gpointer user_data)
384{
385 cmdline *cmd = user_data;
386 if(cmd->argc < 4)
387 {
388 g_print("flasher [flash name] [filename] [source object]\n");
389 g_main_loop_quit(cmd->loop);
390 return;
391 }
392 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
393 ObjectSkeleton *object;
394 manager = g_dbus_object_manager_server_new(dbus_object_path);
395 gchar *s;
396 s = g_strdup_printf("%s/%s",dbus_object_path,cmd->argv[1]);
397
398 object = object_skeleton_new(s);
399 g_free(s);
400
401 FlashControl* flash_control = flash_control_skeleton_new();
402 object_skeleton_set_flash_control(object, flash_control);
403 g_object_unref(flash_control);
404
405 /* Export the object (@manager takes its own reference to @object) */
406 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
407 g_object_unref(object);
408
409 /* Export all objects */
410 g_dbus_object_manager_server_set_connection(manager, connection);
411 bool bmc_flash = false;
412 uint32_t address = 0;
413 if(strcmp(cmd->argv[1],"bmc")==0) {
414 bmc_flash = true;
415 }
416 if(strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
417 bmc_flash = true;
418 address = 0x20300000;
419 }
420 if(strcmp(cmd->argv[1],"bmc_kernel")==0) {
421 bmc_flash = true;
422 address = 0x20080000;
423 }
424
425 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
426 if(rc) {
427 flash_message(connection,cmd->argv[3],"error","Flash Error");
428 } else {
429 flash_message(connection,cmd->argv[3],"done","");
430 }
431
432 //Object exits when done flashing
433 g_main_loop_quit(cmd->loop);
434}
435
436int
437main(int argc, char *argv[])
438{
439 GMainLoop *loop;
440 cmdline cmd;
441 cmd.argc = argc;
442 cmd.argv = argv;
443
444 guint id;
445 loop = g_main_loop_new(NULL, FALSE);
446 cmd.loop = loop;
447
448 id = g_bus_own_name(DBUS_TYPE,
449 dbus_name,
450 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
451 G_BUS_NAME_OWNER_FLAGS_REPLACE,
452 on_bus_acquired,
453 NULL,
454 NULL,
455 &cmd,
456 NULL);
457
458 g_main_loop_run(loop);
459
460 g_bus_unown_name(id);
461 g_main_loop_unref(loop);
462
463 return 0;
464}