blob: 8968867cf33a3bec91a9b6b1161378f10a8aefe2 [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 -040053static int32_t ffs_index = -1;
54
55static uint8_t FLASH_OK = 0;
56static uint8_t FLASH_ERROR = 0x01;
57static uint8_t FLASH_SETUP_ERROR = 0x02;
Brad Bishop77390492016-04-13 10:47:19 -040058
59static int
60erase_chip(void)
61{
62 int rc = 0;
63
64 printf("Erasing... (may take a while !) ");
65 fflush(stdout);
66
Brad Bishop275524e2016-09-08 09:09:55 -040067 rc = arch_flash_erase_chip(bl);
Brad Bishop77390492016-04-13 10:47:19 -040068 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;
Brad Bishop77390492016-04-13 10:47:19 -040083 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 }
Brad Bishop0c82c602016-04-13 13:36:49 -040098 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -040099 method,
100 parm,
101 G_DBUS_CALL_FLAGS_NONE,
102 -1,
103 NULL,
104 &error);
105
106 g_assert_no_error(error);
107}
108
109static int
110program_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;
Brad Bishop275524e2016-09-08 09:09:55 -0400140 rc = blocklevel_write(bl, start, file_buf, len);
Brad Bishop77390492016-04-13 10:47:19 -0400141 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
Brad Bishop77390492016-04-13 10:47:19 -0400169flash_access_cleanup_bmc(void)
170{
171 if(ffsh)
172 ffs_close(ffsh);
Brad Bishop275524e2016-09-08 09:09:55 -0400173 arch_flash_close(bl, NULL);
Brad Bishop77390492016-04-13 10:47:19 -0400174}
175
176static int
Brad Bishop275524e2016-09-08 09:09:55 -0400177flash_access_setup_bmc(bool need_write)
Brad Bishop77390492016-04-13 10:47:19 -0400178{
179 int rc;
180 printf("Setting up BMC flash\n");
Brad Bishop77390492016-04-13 10:47:19 -0400181
Joel Stanley9d612372016-10-31 17:27:23 +1030182 if(arch_flash_bmc(bl, BMC_MTD) != BMC_MTD) {
Brad Bishop275524e2016-09-08 09:09:55 -0400183 fprintf(stderr, "Failed to init flash chip\n");
Brad Bishop77390492016-04-13 10:47:19 -0400184 return FLASH_SETUP_ERROR;
185 }
186
187 /* Setup cleanup function */
188 atexit(flash_access_cleanup_bmc);
189 return FLASH_OK;
190}
191
192static void
193flash_access_cleanup_pnor(void)
194{
195 /* Re-lock flash */
196 if(need_relock)
Brad Bishop275524e2016-09-08 09:09:55 -0400197 arch_flash_set_wrprotect(bl, 1);
Brad Bishop77390492016-04-13 10:47:19 -0400198
Brad Bishop275524e2016-09-08 09:09:55 -0400199 flash_access_cleanup_bmc();
Brad Bishop77390492016-04-13 10:47:19 -0400200}
201
202static int
Brad Bishop275524e2016-09-08 09:09:55 -0400203flash_access_setup_pnor(bool need_write)
Brad Bishop77390492016-04-13 10:47:19 -0400204{
205 int rc;
206 printf("Setting up BIOS flash\n");
207
Brad Bishop275524e2016-09-08 09:09:55 -0400208 /* Create the AST flash controller */
Brad Bishop77390492016-04-13 10:47:19 -0400209
210 /* Open flash chip */
Brad Bishop275524e2016-09-08 09:09:55 -0400211 rc = arch_flash_init(&bl, NULL, true);
Brad Bishop77390492016-04-13 10:47:19 -0400212 if(rc) {
213 fprintf(stderr, "Failed to open flash chip\n");
214 return FLASH_SETUP_ERROR;
215 }
216
217 /* Unlock flash (PNOR only) */
218 if(need_write)
Brad Bishop275524e2016-09-08 09:09:55 -0400219 need_relock = arch_flash_set_wrprotect(bl, 0);
Brad Bishop77390492016-04-13 10:47:19 -0400220
221 /* Setup cleanup function */
222 atexit(flash_access_cleanup_pnor);
223 return FLASH_OK;
224}
225
226uint8_t
227flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
228{
Brad Bishop77390492016-04-13 10:47:19 -0400229 bool erase = true, program = true;
230
231 int rc;
232 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
Brad Bishop77390492016-04-13 10:47:19 -0400233
234 /* Prepare for access */
235 if(bmc_flash) {
Brad Bishop275524e2016-09-08 09:09:55 -0400236 rc = flash_access_setup_bmc(erase || program);
Brad Bishop77390492016-04-13 10:47:19 -0400237 if(rc) {
238 return FLASH_SETUP_ERROR;
239 }
240 } else {
Brad Bishop275524e2016-09-08 09:09:55 -0400241 rc = flash_access_setup_pnor(erase || program);
Brad Bishop77390492016-04-13 10:47:19 -0400242 if(rc) {
243 return FLASH_SETUP_ERROR;
244 }
245 }
246
Brad Bishop77390492016-04-13 10:47:19 -0400247 if(strcmp(write_file,"")!=0)
248 {
249 // If file specified but not size, get size from file
250 struct stat stbuf;
251 if(stat(write_file, &stbuf)) {
252 perror("Failed to get file size");
253 return FLASH_ERROR;
254 }
255 uint32_t write_size = stbuf.st_size;
Brad Bishop77390492016-04-13 10:47:19 -0400256 rc = erase_chip();
257 if(rc) {
258 return FLASH_ERROR;
259 }
260 rc = program_file(flash_control, write_file, address, write_size);
261 if(rc) {
262 return FLASH_ERROR;
263 }
Brad Bishop77390492016-04-13 10:47:19 -0400264
265 printf("Flash done\n");
266 }
267 else
268 {
269 printf("Flash tuned\n");
270 }
271 return FLASH_OK;
272}
273
274static void
275on_bus_acquired(GDBusConnection *connection,
276 const gchar *name,
277 gpointer user_data)
278{
279 cmdline *cmd = user_data;
280 if(cmd->argc < 4)
281 {
282 g_print("flasher [flash name] [filename] [source object]\n");
283 g_main_loop_quit(cmd->loop);
284 return;
285 }
286 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
287 ObjectSkeleton *object;
288 manager = g_dbus_object_manager_server_new(dbus_object_path);
289 gchar *s;
290 s = g_strdup_printf("%s/%s",dbus_object_path,cmd->argv[1]);
291
292 object = object_skeleton_new(s);
293 g_free(s);
294
295 FlashControl* flash_control = flash_control_skeleton_new();
296 object_skeleton_set_flash_control(object, flash_control);
297 g_object_unref(flash_control);
298
299 /* Export the object (@manager takes its own reference to @object) */
300 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
301 g_object_unref(object);
302
303 /* Export all objects */
304 g_dbus_object_manager_server_set_connection(manager, connection);
305 bool bmc_flash = false;
306 uint32_t address = 0;
307 if(strcmp(cmd->argv[1],"bmc")==0) {
308 bmc_flash = true;
309 }
310 if(strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
311 bmc_flash = true;
312 address = 0x20300000;
313 }
314 if(strcmp(cmd->argv[1],"bmc_kernel")==0) {
315 bmc_flash = true;
316 address = 0x20080000;
317 }
318
319 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
320 if(rc) {
321 flash_message(connection,cmd->argv[3],"error","Flash Error");
322 } else {
323 flash_message(connection,cmd->argv[3],"done","");
324 }
325
326 //Object exits when done flashing
327 g_main_loop_quit(cmd->loop);
328}
329
330int
331main(int argc, char *argv[])
332{
333 GMainLoop *loop;
334 cmdline cmd;
335 cmd.argc = argc;
336 cmd.argv = argv;
337
338 guint id;
339 loop = g_main_loop_new(NULL, FALSE);
340 cmd.loop = loop;
341
342 id = g_bus_own_name(DBUS_TYPE,
343 dbus_name,
344 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
345 G_BUS_NAME_OWNER_FLAGS_REPLACE,
346 on_bus_acquired,
347 NULL,
348 NULL,
349 &cmd,
350 NULL);
351
352 g_main_loop_run(loop);
353
354 g_bus_unown_name(id);
355 g_main_loop_unref(loop);
356
357 return 0;
358}