blob: 26bf44139436506c7ae947c2e13496c5cb6f74fc [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;
55static uint8_t FLASH_ERROR = 1;
Norman James7b1afb62015-10-28 20:28:37 -050056static struct blocklevel_device *bl;
Norman Jamesc0d94e02015-10-13 10:06:12 -050057
58static 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 James7b1afb62015-10-28 20:28:37 -050075
Norman James166acf42015-10-22 07:11:51 -050076void 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 Jamesc0d94e02015-10-13 10:06:12 -0500109static 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
167static 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}
203static 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
212static void flash_access_setup_bmc(bool use_lpc, bool need_write)
213{
214 int rc;
Norman James166acf42015-10-22 07:11:51 -0500215 printf("Setting up BMC flash\n");
Norman Jamesc0d94e02015-10-13 10:06:12 -0500216 /* 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
237static 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
257static void flash_access_setup_pnor(bool use_lpc, bool use_sfc, bool need_write)
258{
259 int rc;
Norman James166acf42015-10-22 07:11:51 -0500260 printf("Setting up BIOS flash\n");
Norman Jamesc0d94e02015-10-13 10:06:12 -0500261
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 James15aaa802015-10-28 06:54:48 -0500301uint8_t flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
Norman Jamesc0d94e02015-10-13 10:06:12 -0500302{
303 bool has_sfc = false, has_ast = false, use_lpc = true;
304 bool erase = true, program = true;
Norman James15aaa802015-10-28 06:54:48 -0500305
Norman Jamesc0d94e02015-10-13 10:06:12 -0500306 int rc;
Norman James15aaa802015-10-28 06:54:48 -0500307 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
Norman Jamesc0d94e02015-10-13 10:06:12 -0500308#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 James166acf42015-10-22 07:11:51 -0500316 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500317 }
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 James166acf42015-10-22 07:11:51 -0500322 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500323 }
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 James166acf42015-10-22 07:11:51 -0500331 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500332 }
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 James166acf42015-10-22 07:11:51 -0500340 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500341 }
342 uint32_t write_size = stbuf.st_size;
343#ifdef __arm__
344 rc = erase_chip();
345 if (rc) {
Norman James166acf42015-10-22 07:11:51 -0500346 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500347 }
348 rc = program_file(flash_control, write_file, address, write_size);
349 if (rc) {
Norman James166acf42015-10-22 07:11:51 -0500350 return FLASH_ERROR;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500351 }
352#endif
353
Norman Jamesc0d94e02015-10-13 10:06:12 -0500354 printf("Flash done\n");
355 }
356 else
357 {
Norman Jamesc0d94e02015-10-13 10:06:12 -0500358 printf("Flash tuned\n");
359 }
Norman James166acf42015-10-22 07:11:51 -0500360 return FLASH_OK;
Norman Jamesc0d94e02015-10-13 10:06:12 -0500361}
362
363static void
364on_bus_acquired (GDBusConnection *connection,
365 const gchar *name,
366 gpointer user_data)
367{
368
369 cmdline *cmd = user_data;
Norman James166acf42015-10-22 07:11:51 -0500370 if (cmd->argc < 4)
Norman Jamesc0d94e02015-10-13 10:06:12 -0500371 {
Norman James166acf42015-10-22 07:11:51 -0500372 g_print("flasher [flash name] [filename] [source object]\n");
Norman Jamesc0d94e02015-10-13 10:06:12 -0500373 return;
374 }
Norman James166acf42015-10-22 07:11:51 -0500375 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
Norman Jamesc0d94e02015-10-13 10:06:12 -0500376 ObjectSkeleton *object;
377 manager = g_dbus_object_manager_server_new (dbus_object_path);
378 gchar *s;
379 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[1]);
380
381 object = object_skeleton_new (s);
382 g_free (s);
383
384 FlashControl* flash_control = flash_control_skeleton_new ();
385 object_skeleton_set_flash_control (object, flash_control);
386 g_object_unref (flash_control);
387
388 /* Export the object (@manager takes its own reference to @object) */
389 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
390 g_object_unref (object);
391
392 /* Export all objects */
393 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James166acf42015-10-22 07:11:51 -0500394 bool bmc_flash = false;
Norman James15aaa802015-10-28 06:54:48 -0500395 uint32_t address = 0;
Norman James166acf42015-10-22 07:11:51 -0500396 if (strcmp(cmd->argv[1],"bmc")==0) {
397 bmc_flash = true;
398 }
Norman James15aaa802015-10-28 06:54:48 -0500399 if (strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
400 bmc_flash = true;
401 address = 0x20300000;
402 }
403 if (strcmp(cmd->argv[1],"bmc_kernel")==0) {
404 bmc_flash = true;
405 address = 0x20080000;
406 }
407
408 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
Norman James166acf42015-10-22 07:11:51 -0500409 if (rc == FLASH_ERROR) {
410 flash_message(connection,cmd->argv[3],"error","Flash Error");
411 } else {
412 flash_message(connection,cmd->argv[3],"done","");
413 }
Norman Jamesc0d94e02015-10-13 10:06:12 -0500414
415 //Object exits when done flashing
416 g_main_loop_quit(cmd->loop);
417}
418
419int main(int argc, char *argv[])
420{
421
422 GMainLoop *loop;
423 cmdline cmd;
424 cmd.argc = argc;
425 cmd.argv = argv;
426
427 guint id;
428 loop = g_main_loop_new (NULL, FALSE);
429 cmd.loop = loop;
430
431 id = g_bus_own_name (DBUS_TYPE,
432 dbus_name,
433 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
434 G_BUS_NAME_OWNER_FLAGS_REPLACE,
435 on_bus_acquired,
436 NULL,
437 NULL,
438 &cmd,
439 NULL);
440
441 g_main_loop_run (loop);
442
443 g_bus_unown_name (id);
444 g_main_loop_unref (loop);
445
446 return 0;
447}