blob: 874d21b0914e3c86b364a691cc01d7df67aa8ed9 [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;
Brad Bishop77390492016-04-13 10:47:19 -040053
54static uint8_t FLASH_OK = 0;
55static uint8_t FLASH_ERROR = 0x01;
56static uint8_t FLASH_SETUP_ERROR = 0x02;
Brad Bishop77390492016-04-13 10:47:19 -040057
58static int
59erase_chip(void)
60{
61 int rc = 0;
62
63 printf("Erasing... (may take a while !) ");
64 fflush(stdout);
65
Brad Bishop275524e2016-09-08 09:09:55 -040066 rc = arch_flash_erase_chip(bl);
Brad Bishop77390492016-04-13 10:47:19 -040067 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
76void
77flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg)
78{
79 GDBusProxy *proxy;
80 GError *error;
81 GVariant *parm = NULL;
Brad Bishop77390492016-04-13 10:47:19 -040082 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 }
Brad Bishop0c82c602016-04-13 13:36:49 -040097 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -040098 method,
99 parm,
100 G_DBUS_CALL_FLAGS_NONE,
101 -1,
102 NULL,
103 &error);
104
105 g_assert_no_error(error);
106}
107
108static int
109program_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;
Brad Bishop275524e2016-09-08 09:09:55 -0400139 rc = blocklevel_write(bl, start, file_buf, len);
Brad Bishop77390492016-04-13 10:47:19 -0400140 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
Brad Bishop77390492016-04-13 10:47:19 -0400159 return(0);
160}
161
162static void
Brad Bishop77390492016-04-13 10:47:19 -0400163flash_access_cleanup_bmc(void)
164{
165 if(ffsh)
166 ffs_close(ffsh);
Brad Bishop275524e2016-09-08 09:09:55 -0400167 arch_flash_close(bl, NULL);
Brad Bishop77390492016-04-13 10:47:19 -0400168}
169
170static int
Brad Bishop275524e2016-09-08 09:09:55 -0400171flash_access_setup_bmc(bool need_write)
Brad Bishop77390492016-04-13 10:47:19 -0400172{
173 int rc;
174 printf("Setting up BMC flash\n");
Brad Bishop77390492016-04-13 10:47:19 -0400175
Joel Stanley9d612372016-10-31 17:27:23 +1030176 if(arch_flash_bmc(bl, BMC_MTD) != BMC_MTD) {
Brad Bishop275524e2016-09-08 09:09:55 -0400177 fprintf(stderr, "Failed to init flash chip\n");
Brad Bishop77390492016-04-13 10:47:19 -0400178 return FLASH_SETUP_ERROR;
179 }
180
181 /* Setup cleanup function */
182 atexit(flash_access_cleanup_bmc);
183 return FLASH_OK;
184}
185
186static void
187flash_access_cleanup_pnor(void)
188{
189 /* Re-lock flash */
190 if(need_relock)
Brad Bishop275524e2016-09-08 09:09:55 -0400191 arch_flash_set_wrprotect(bl, 1);
Brad Bishop77390492016-04-13 10:47:19 -0400192
Brad Bishop275524e2016-09-08 09:09:55 -0400193 flash_access_cleanup_bmc();
Brad Bishop77390492016-04-13 10:47:19 -0400194}
195
196static int
Brad Bishop275524e2016-09-08 09:09:55 -0400197flash_access_setup_pnor(bool need_write)
Brad Bishop77390492016-04-13 10:47:19 -0400198{
199 int rc;
200 printf("Setting up BIOS flash\n");
201
Brad Bishop275524e2016-09-08 09:09:55 -0400202 /* Create the AST flash controller */
Brad Bishop77390492016-04-13 10:47:19 -0400203
204 /* Open flash chip */
Brad Bishop275524e2016-09-08 09:09:55 -0400205 rc = arch_flash_init(&bl, NULL, true);
Brad Bishop77390492016-04-13 10:47:19 -0400206 if(rc) {
207 fprintf(stderr, "Failed to open flash chip\n");
208 return FLASH_SETUP_ERROR;
209 }
210
211 /* Unlock flash (PNOR only) */
212 if(need_write)
Brad Bishop275524e2016-09-08 09:09:55 -0400213 need_relock = arch_flash_set_wrprotect(bl, 0);
Brad Bishop77390492016-04-13 10:47:19 -0400214
215 /* Setup cleanup function */
216 atexit(flash_access_cleanup_pnor);
217 return FLASH_OK;
218}
219
220uint8_t
221flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
222{
Brad Bishop77390492016-04-13 10:47:19 -0400223 bool erase = true, program = true;
224
225 int rc;
226 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
Brad Bishop77390492016-04-13 10:47:19 -0400227
228 /* Prepare for access */
229 if(bmc_flash) {
Brad Bishop275524e2016-09-08 09:09:55 -0400230 rc = flash_access_setup_bmc(erase || program);
Brad Bishop77390492016-04-13 10:47:19 -0400231 if(rc) {
232 return FLASH_SETUP_ERROR;
233 }
234 } else {
Brad Bishop275524e2016-09-08 09:09:55 -0400235 rc = flash_access_setup_pnor(erase || program);
Brad Bishop77390492016-04-13 10:47:19 -0400236 if(rc) {
237 return FLASH_SETUP_ERROR;
238 }
239 }
240
Brad Bishop77390492016-04-13 10:47:19 -0400241 if(strcmp(write_file,"")!=0)
242 {
243 // If file specified but not size, get size from file
244 struct stat stbuf;
245 if(stat(write_file, &stbuf)) {
246 perror("Failed to get file size");
247 return FLASH_ERROR;
248 }
249 uint32_t write_size = stbuf.st_size;
Brad Bishop77390492016-04-13 10:47:19 -0400250 rc = erase_chip();
251 if(rc) {
252 return FLASH_ERROR;
253 }
254 rc = program_file(flash_control, write_file, address, write_size);
255 if(rc) {
256 return FLASH_ERROR;
257 }
Brad Bishop77390492016-04-13 10:47:19 -0400258
259 printf("Flash done\n");
260 }
261 else
262 {
263 printf("Flash tuned\n");
264 }
265 return FLASH_OK;
266}
267
268static void
269on_bus_acquired(GDBusConnection *connection,
270 const gchar *name,
271 gpointer user_data)
272{
273 cmdline *cmd = user_data;
274 if(cmd->argc < 4)
275 {
276 g_print("flasher [flash name] [filename] [source object]\n");
277 g_main_loop_quit(cmd->loop);
278 return;
279 }
280 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
281 ObjectSkeleton *object;
282 manager = g_dbus_object_manager_server_new(dbus_object_path);
283 gchar *s;
284 s = g_strdup_printf("%s/%s",dbus_object_path,cmd->argv[1]);
285
286 object = object_skeleton_new(s);
287 g_free(s);
288
289 FlashControl* flash_control = flash_control_skeleton_new();
290 object_skeleton_set_flash_control(object, flash_control);
291 g_object_unref(flash_control);
292
293 /* Export the object (@manager takes its own reference to @object) */
294 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
295 g_object_unref(object);
296
297 /* Export all objects */
298 g_dbus_object_manager_server_set_connection(manager, connection);
299 bool bmc_flash = false;
300 uint32_t address = 0;
301 if(strcmp(cmd->argv[1],"bmc")==0) {
302 bmc_flash = true;
303 }
304 if(strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
305 bmc_flash = true;
306 address = 0x20300000;
307 }
308 if(strcmp(cmd->argv[1],"bmc_kernel")==0) {
309 bmc_flash = true;
310 address = 0x20080000;
311 }
312
313 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
314 if(rc) {
315 flash_message(connection,cmd->argv[3],"error","Flash Error");
316 } else {
317 flash_message(connection,cmd->argv[3],"done","");
318 }
319
320 //Object exits when done flashing
321 g_main_loop_quit(cmd->loop);
322}
323
324int
325main(int argc, char *argv[])
326{
327 GMainLoop *loop;
328 cmdline cmd;
329 cmd.argc = argc;
330 cmd.argv = argv;
331
332 guint id;
333 loop = g_main_loop_new(NULL, FALSE);
334 cmd.loop = loop;
335
336 id = g_bus_own_name(DBUS_TYPE,
337 dbus_name,
338 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
339 G_BUS_NAME_OWNER_FLAGS_REPLACE,
340 on_bus_acquired,
341 NULL,
342 NULL,
343 &cmd,
344 NULL);
345
346 g_main_loop_run(loop);
347
348 g_bus_unown_name(id);
349 g_main_loop_unref(loop);
350
351 return 0;
352}