blob: 747ad0bcb958c0385f61ffde10700515691f28f3 [file] [log] [blame]
Joel Stanley7b62ce92016-10-31 17:27:13 +10301/* 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 Bishop77390492016-04-13 10:47:19 -040017#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 Bishop275524e2016-09-08 09:09:55 -040032#include <libflash/arch_flash.h>
Brad Bishop77390492016-04-13 10:47:19 -040033#include <libflash/libffs.h>
Brad Bishop275524e2016-09-08 09:09:55 -040034#include <libflash/blocklevel.h>
35#include <libflash/errors.h>
Brad Bishopf6c85682016-06-27 11:56:39 -040036#include <openbmc_intf.h>
37#include <openbmc.h>
Brad Bishop77390492016-04-13 10:47:19 -040038
39static const gchar* dbus_object_path = "/org/openbmc/control";
Brad Bishop77390492016-04-13 10:47:19 -040040static const gchar* dbus_name = "org.openbmc.control.Flasher";
41
42static GDBusObjectManagerServer *manager = NULL;
43
44#define __aligned(x) __attribute__((aligned(x)))
45
Brad Bishop77390492016-04-13 10:47:19 -040046static bool need_relock;
Brad Bishop77390492016-04-13 10:47:19 -040047
48#define FILE_BUF_SIZE 0x10000
49static uint8_t file_buf[FILE_BUF_SIZE] __aligned(0x1000);
50
Brad Bishop275524e2016-09-08 09:09:55 -040051static struct blocklevel_device *bl;
Brad Bishop77390492016-04-13 10:47:19 -040052static struct ffs_handle *ffsh;
53static uint32_t fl_total_size, fl_erase_granule;
54static const char *fl_name;
55static int32_t ffs_index = -1;
56
57static uint8_t FLASH_OK = 0;
58static uint8_t FLASH_ERROR = 0x01;
59static uint8_t FLASH_SETUP_ERROR = 0x02;
Brad Bishop77390492016-04-13 10:47:19 -040060
61static int
62erase_chip(void)
63{
64 int rc = 0;
65
66 printf("Erasing... (may take a while !) ");
67 fflush(stdout);
68
Brad Bishop275524e2016-09-08 09:09:55 -040069 rc = arch_flash_erase_chip(bl);
Brad Bishop77390492016-04-13 10:47:19 -040070 if(rc) {
71 fprintf(stderr, "Error %d erasing chip\n", rc);
72 return(rc);
73 }
74
75 printf("done !\n");
76 return(rc);
77}
78
79void
80flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg)
81{
82 GDBusProxy *proxy;
83 GError *error;
84 GVariant *parm = NULL;
Brad Bishop77390492016-04-13 10:47:19 -040085 error = NULL;
86 proxy = g_dbus_proxy_new_sync(connection,
87 G_DBUS_PROXY_FLAGS_NONE,
88 NULL, /* GDBusInterfaceInfo* */
89 "org.openbmc.control.Flash", /* name */
90 obj_path, /* object path */
91 "org.openbmc.Flash", /* interface name */
92 NULL, /* GCancellable */
93 &error);
94 g_assert_no_error(error);
95
96 error = NULL;
97 if(strcmp(method,"error")==0) {
98 parm = g_variant_new("(s)",error_msg);
99 }
Brad Bishop0c82c602016-04-13 13:36:49 -0400100 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -0400101 method,
102 parm,
103 G_DBUS_CALL_FLAGS_NONE,
104 -1,
105 NULL,
106 &error);
107
108 g_assert_no_error(error);
109}
110
111static int
112program_file(FlashControl* flash_control, const char *file, uint32_t start, uint32_t size)
113{
114 int fd, rc;
115 ssize_t len;
116 uint32_t actual_size = 0;
117
118 fd = open(file, O_RDONLY);
119 if(fd == -1) {
120 perror("Failed to open file");
121 return(fd);
122 }
123 printf("About to program \"%s\" at 0x%08x..0x%08x !\n",
124 file, start, size);
125
126 printf("Programming & Verifying...\n");
127 //progress_init(size >> 8);
128 unsigned int save_size = size;
129 uint8_t last_progress = 0;
130 while(size) {
131 len = read(fd, file_buf, FILE_BUF_SIZE);
132 if(len < 0) {
133 perror("Error reading file");
134 return(1);
135 }
136 if(len == 0)
137 break;
138 if(len > size)
139 len = size;
140 size -= len;
141 actual_size += len;
Brad Bishop275524e2016-09-08 09:09:55 -0400142 rc = blocklevel_write(bl, start, file_buf, len);
Brad Bishop77390492016-04-13 10:47:19 -0400143 if(rc) {
144 if(rc == FLASH_ERR_VERIFY_FAILURE)
145 fprintf(stderr, "Verification failed for"
146 " chunk at 0x%08x\n", start);
147 else
148 fprintf(stderr, "Flash write error %d for"
149 " chunk at 0x%08x\n", rc, start);
150 return(rc);
151 }
152 start += len;
153 unsigned int percent = (100*actual_size/save_size);
154 uint8_t progress = (uint8_t)(percent);
155 if(progress != last_progress) {
156 flash_control_emit_progress(flash_control,file,progress);
157 last_progress = progress;
158 }
159 }
160 close(fd);
161
162 /* If this is a flash partition, adjust its size */
163 if(ffsh && ffs_index >= 0) {
164 printf("Updating actual size in partition header...\n");
165 ffs_update_act_size(ffsh, ffs_index, actual_size);
166 }
167 return(0);
168}
169
170static void
Brad Bishop77390492016-04-13 10:47:19 -0400171flash_access_cleanup_bmc(void)
172{
173 if(ffsh)
174 ffs_close(ffsh);
Brad Bishop275524e2016-09-08 09:09:55 -0400175 arch_flash_close(bl, NULL);
Brad Bishop77390492016-04-13 10:47:19 -0400176}
177
178static int
Brad Bishop275524e2016-09-08 09:09:55 -0400179flash_access_setup_bmc(bool need_write)
Brad Bishop77390492016-04-13 10:47:19 -0400180{
181 int rc;
182 printf("Setting up BMC flash\n");
Brad Bishop77390492016-04-13 10:47:19 -0400183
Joel Stanley9d612372016-10-31 17:27:23 +1030184 if(arch_flash_bmc(bl, BMC_MTD) != BMC_MTD) {
Brad Bishop275524e2016-09-08 09:09:55 -0400185 fprintf(stderr, "Failed to init flash chip\n");
Brad Bishop77390492016-04-13 10:47:19 -0400186 return FLASH_SETUP_ERROR;
187 }
188
189 /* Setup cleanup function */
190 atexit(flash_access_cleanup_bmc);
191 return FLASH_OK;
192}
193
194static void
195flash_access_cleanup_pnor(void)
196{
197 /* Re-lock flash */
198 if(need_relock)
Brad Bishop275524e2016-09-08 09:09:55 -0400199 arch_flash_set_wrprotect(bl, 1);
Brad Bishop77390492016-04-13 10:47:19 -0400200
Brad Bishop275524e2016-09-08 09:09:55 -0400201 flash_access_cleanup_bmc();
Brad Bishop77390492016-04-13 10:47:19 -0400202}
203
204static int
Brad Bishop275524e2016-09-08 09:09:55 -0400205flash_access_setup_pnor(bool need_write)
Brad Bishop77390492016-04-13 10:47:19 -0400206{
207 int rc;
208 printf("Setting up BIOS flash\n");
209
Brad Bishop275524e2016-09-08 09:09:55 -0400210 /* Create the AST flash controller */
Brad Bishop77390492016-04-13 10:47:19 -0400211
212 /* Open flash chip */
Brad Bishop275524e2016-09-08 09:09:55 -0400213 rc = arch_flash_init(&bl, NULL, true);
Brad Bishop77390492016-04-13 10:47:19 -0400214 if(rc) {
215 fprintf(stderr, "Failed to open flash chip\n");
216 return FLASH_SETUP_ERROR;
217 }
218
219 /* Unlock flash (PNOR only) */
220 if(need_write)
Brad Bishop275524e2016-09-08 09:09:55 -0400221 need_relock = arch_flash_set_wrprotect(bl, 0);
Brad Bishop77390492016-04-13 10:47:19 -0400222
223 /* Setup cleanup function */
224 atexit(flash_access_cleanup_pnor);
225 return FLASH_OK;
226}
227
228uint8_t
229flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
230{
Brad Bishop77390492016-04-13 10:47:19 -0400231 bool erase = true, program = true;
232
233 int rc;
234 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
Brad Bishop77390492016-04-13 10:47:19 -0400235
236 /* Prepare for access */
237 if(bmc_flash) {
Brad Bishop275524e2016-09-08 09:09:55 -0400238 rc = flash_access_setup_bmc(erase || program);
Brad Bishop77390492016-04-13 10:47:19 -0400239 if(rc) {
240 return FLASH_SETUP_ERROR;
241 }
242 } else {
Brad Bishop275524e2016-09-08 09:09:55 -0400243 rc = flash_access_setup_pnor(erase || program);
Brad Bishop77390492016-04-13 10:47:19 -0400244 if(rc) {
245 return FLASH_SETUP_ERROR;
246 }
247 }
248
Brad Bishop275524e2016-09-08 09:09:55 -0400249 rc = blocklevel_get_info(bl, &fl_name,
Brad Bishop77390492016-04-13 10:47:19 -0400250 &fl_total_size, &fl_erase_granule);
251 if(rc) {
252 fprintf(stderr, "Error %d getting flash info\n", rc);
253 return FLASH_SETUP_ERROR;
254 }
Brad Bishop77390492016-04-13 10:47:19 -0400255 if(strcmp(write_file,"")!=0)
256 {
257 // If file specified but not size, get size from file
258 struct stat stbuf;
259 if(stat(write_file, &stbuf)) {
260 perror("Failed to get file size");
261 return FLASH_ERROR;
262 }
263 uint32_t write_size = stbuf.st_size;
Brad Bishop77390492016-04-13 10:47:19 -0400264 rc = erase_chip();
265 if(rc) {
266 return FLASH_ERROR;
267 }
268 rc = program_file(flash_control, write_file, address, write_size);
269 if(rc) {
270 return FLASH_ERROR;
271 }
Brad Bishop77390492016-04-13 10:47:19 -0400272
273 printf("Flash done\n");
274 }
275 else
276 {
277 printf("Flash tuned\n");
278 }
279 return FLASH_OK;
280}
281
282static void
283on_bus_acquired(GDBusConnection *connection,
284 const gchar *name,
285 gpointer user_data)
286{
287 cmdline *cmd = user_data;
288 if(cmd->argc < 4)
289 {
290 g_print("flasher [flash name] [filename] [source object]\n");
291 g_main_loop_quit(cmd->loop);
292 return;
293 }
294 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
295 ObjectSkeleton *object;
296 manager = g_dbus_object_manager_server_new(dbus_object_path);
297 gchar *s;
298 s = g_strdup_printf("%s/%s",dbus_object_path,cmd->argv[1]);
299
300 object = object_skeleton_new(s);
301 g_free(s);
302
303 FlashControl* flash_control = flash_control_skeleton_new();
304 object_skeleton_set_flash_control(object, flash_control);
305 g_object_unref(flash_control);
306
307 /* Export the object (@manager takes its own reference to @object) */
308 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
309 g_object_unref(object);
310
311 /* Export all objects */
312 g_dbus_object_manager_server_set_connection(manager, connection);
313 bool bmc_flash = false;
314 uint32_t address = 0;
315 if(strcmp(cmd->argv[1],"bmc")==0) {
316 bmc_flash = true;
317 }
318 if(strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
319 bmc_flash = true;
320 address = 0x20300000;
321 }
322 if(strcmp(cmd->argv[1],"bmc_kernel")==0) {
323 bmc_flash = true;
324 address = 0x20080000;
325 }
326
327 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
328 if(rc) {
329 flash_message(connection,cmd->argv[3],"error","Flash Error");
330 } else {
331 flash_message(connection,cmd->argv[3],"done","");
332 }
333
334 //Object exits when done flashing
335 g_main_loop_quit(cmd->loop);
336}
337
338int
339main(int argc, char *argv[])
340{
341 GMainLoop *loop;
342 cmdline cmd;
343 cmd.argc = argc;
344 cmd.argv = argv;
345
346 guint id;
347 loop = g_main_loop_new(NULL, FALSE);
348 cmd.loop = loop;
349
350 id = g_bus_own_name(DBUS_TYPE,
351 dbus_name,
352 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
353 G_BUS_NAME_OWNER_FLAGS_REPLACE,
354 on_bus_acquired,
355 NULL,
356 NULL,
357 &cmd,
358 NULL);
359
360 g_main_loop_run(loop);
361
362 g_bus_unown_name(id);
363 g_main_loop_unref(loop);
364
365 return 0;
366}