blob: 984565c2fa77fe487c52d19e8c8047688b833d76 [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 -040046#define FILE_BUF_SIZE 0x10000
47static uint8_t file_buf[FILE_BUF_SIZE] __aligned(0x1000);
48
Brad Bishop275524e2016-09-08 09:09:55 -040049static struct blocklevel_device *bl;
Brad Bishop77390492016-04-13 10:47:19 -040050static struct ffs_handle *ffsh;
Brad Bishop77390492016-04-13 10:47:19 -040051
52static uint8_t FLASH_OK = 0;
53static uint8_t FLASH_ERROR = 0x01;
54static uint8_t FLASH_SETUP_ERROR = 0x02;
Brad Bishop77390492016-04-13 10:47:19 -040055
56static int
57erase_chip(void)
58{
59 int rc = 0;
60
61 printf("Erasing... (may take a while !) ");
62 fflush(stdout);
63
Brad Bishop275524e2016-09-08 09:09:55 -040064 rc = arch_flash_erase_chip(bl);
Brad Bishop77390492016-04-13 10:47:19 -040065 if(rc) {
66 fprintf(stderr, "Error %d erasing chip\n", rc);
67 return(rc);
68 }
69
70 printf("done !\n");
71 return(rc);
72}
73
74void
75flash_message(GDBusConnection* connection,char* obj_path,char* method, char* error_msg)
76{
77 GDBusProxy *proxy;
78 GError *error;
79 GVariant *parm = NULL;
Brad Bishop77390492016-04-13 10:47:19 -040080 error = NULL;
81 proxy = g_dbus_proxy_new_sync(connection,
82 G_DBUS_PROXY_FLAGS_NONE,
83 NULL, /* GDBusInterfaceInfo* */
84 "org.openbmc.control.Flash", /* name */
85 obj_path, /* object path */
86 "org.openbmc.Flash", /* interface name */
87 NULL, /* GCancellable */
88 &error);
89 g_assert_no_error(error);
90
91 error = NULL;
92 if(strcmp(method,"error")==0) {
93 parm = g_variant_new("(s)",error_msg);
94 }
Brad Bishop0c82c602016-04-13 13:36:49 -040095 g_dbus_proxy_call_sync(proxy,
Brad Bishop77390492016-04-13 10:47:19 -040096 method,
97 parm,
98 G_DBUS_CALL_FLAGS_NONE,
99 -1,
100 NULL,
101 &error);
102
103 g_assert_no_error(error);
104}
105
106static int
107program_file(FlashControl* flash_control, const char *file, uint32_t start, uint32_t size)
108{
109 int fd, rc;
110 ssize_t len;
111 uint32_t actual_size = 0;
112
113 fd = open(file, O_RDONLY);
114 if(fd == -1) {
115 perror("Failed to open file");
116 return(fd);
117 }
118 printf("About to program \"%s\" at 0x%08x..0x%08x !\n",
119 file, start, size);
120
121 printf("Programming & Verifying...\n");
122 //progress_init(size >> 8);
123 unsigned int save_size = size;
124 uint8_t last_progress = 0;
125 while(size) {
126 len = read(fd, file_buf, FILE_BUF_SIZE);
127 if(len < 0) {
128 perror("Error reading file");
129 return(1);
130 }
131 if(len == 0)
132 break;
133 if(len > size)
134 len = size;
135 size -= len;
136 actual_size += len;
Brad Bishop275524e2016-09-08 09:09:55 -0400137 rc = blocklevel_write(bl, start, file_buf, len);
Brad Bishop77390492016-04-13 10:47:19 -0400138 if(rc) {
139 if(rc == FLASH_ERR_VERIFY_FAILURE)
140 fprintf(stderr, "Verification failed for"
141 " chunk at 0x%08x\n", start);
142 else
143 fprintf(stderr, "Flash write error %d for"
144 " chunk at 0x%08x\n", rc, start);
145 return(rc);
146 }
147 start += len;
148 unsigned int percent = (100*actual_size/save_size);
149 uint8_t progress = (uint8_t)(percent);
150 if(progress != last_progress) {
151 flash_control_emit_progress(flash_control,file,progress);
152 last_progress = progress;
153 }
154 }
155 close(fd);
156
Brad Bishop77390492016-04-13 10:47:19 -0400157 return(0);
158}
159
160static void
Joel Stanley7ba41722016-11-03 12:23:11 +1030161flash_access_cleanup(void)
Brad Bishop77390492016-04-13 10:47:19 -0400162{
163 if(ffsh)
164 ffs_close(ffsh);
Brad Bishop275524e2016-09-08 09:09:55 -0400165 arch_flash_close(bl, NULL);
Brad Bishop77390492016-04-13 10:47:19 -0400166}
167
168static int
Joel Stanley66777052016-11-03 12:21:24 +1030169flash_access_setup_bmc(void)
Brad Bishop77390492016-04-13 10:47:19 -0400170{
171 int rc;
172 printf("Setting up BMC flash\n");
Brad Bishop77390492016-04-13 10:47:19 -0400173
Joel Stanley9d612372016-10-31 17:27:23 +1030174 if(arch_flash_bmc(bl, BMC_MTD) != BMC_MTD) {
Brad Bishop275524e2016-09-08 09:09:55 -0400175 fprintf(stderr, "Failed to init flash chip\n");
Brad Bishop77390492016-04-13 10:47:19 -0400176 return FLASH_SETUP_ERROR;
177 }
178
179 /* Setup cleanup function */
Joel Stanley7ba41722016-11-03 12:23:11 +1030180 atexit(flash_access_cleanup);
Brad Bishop77390492016-04-13 10:47:19 -0400181 return FLASH_OK;
182}
183
Brad Bishop77390492016-04-13 10:47:19 -0400184static int
Joel Stanley66777052016-11-03 12:21:24 +1030185flash_access_setup_pnor(void)
Brad Bishop77390492016-04-13 10:47:19 -0400186{
187 int rc;
188 printf("Setting up BIOS flash\n");
189
Brad Bishop275524e2016-09-08 09:09:55 -0400190 /* Create the AST flash controller */
Brad Bishop77390492016-04-13 10:47:19 -0400191
192 /* Open flash chip */
Brad Bishop275524e2016-09-08 09:09:55 -0400193 rc = arch_flash_init(&bl, NULL, true);
Brad Bishop77390492016-04-13 10:47:19 -0400194 if(rc) {
195 fprintf(stderr, "Failed to open flash chip\n");
196 return FLASH_SETUP_ERROR;
197 }
198
Brad Bishop77390492016-04-13 10:47:19 -0400199 /* Setup cleanup function */
Joel Stanley7ba41722016-11-03 12:23:11 +1030200 atexit(flash_access_cleanup);
Brad Bishop77390492016-04-13 10:47:19 -0400201 return FLASH_OK;
202}
203
204uint8_t
205flash(FlashControl* flash_control,bool bmc_flash, uint32_t address, char* write_file, char* obj_path)
206{
Brad Bishop77390492016-04-13 10:47:19 -0400207 int rc;
208 printf("flasher: %s, BMC = %d, address = 0x%x\n",write_file,bmc_flash,address);
Brad Bishop77390492016-04-13 10:47:19 -0400209
210 /* Prepare for access */
211 if(bmc_flash) {
Joel Stanley66777052016-11-03 12:21:24 +1030212 rc = flash_access_setup_bmc();
Brad Bishop77390492016-04-13 10:47:19 -0400213 if(rc) {
214 return FLASH_SETUP_ERROR;
215 }
216 } else {
Joel Stanley66777052016-11-03 12:21:24 +1030217 rc = flash_access_setup_pnor();
Brad Bishop77390492016-04-13 10:47:19 -0400218 if(rc) {
219 return FLASH_SETUP_ERROR;
220 }
221 }
222
Brad Bishop77390492016-04-13 10:47:19 -0400223 if(strcmp(write_file,"")!=0)
224 {
225 // If file specified but not size, get size from file
226 struct stat stbuf;
227 if(stat(write_file, &stbuf)) {
228 perror("Failed to get file size");
229 return FLASH_ERROR;
230 }
231 uint32_t write_size = stbuf.st_size;
Brad Bishop77390492016-04-13 10:47:19 -0400232 rc = erase_chip();
233 if(rc) {
234 return FLASH_ERROR;
235 }
236 rc = program_file(flash_control, write_file, address, write_size);
237 if(rc) {
238 return FLASH_ERROR;
239 }
Brad Bishop77390492016-04-13 10:47:19 -0400240
241 printf("Flash done\n");
242 }
243 else
244 {
245 printf("Flash tuned\n");
246 }
247 return FLASH_OK;
248}
249
250static void
251on_bus_acquired(GDBusConnection *connection,
252 const gchar *name,
253 gpointer user_data)
254{
255 cmdline *cmd = user_data;
256 if(cmd->argc < 4)
257 {
258 g_print("flasher [flash name] [filename] [source object]\n");
259 g_main_loop_quit(cmd->loop);
260 return;
261 }
262 printf("Starting flasher: %s,%s,%s,\n",cmd->argv[1],cmd->argv[2],cmd->argv[3]);
263 ObjectSkeleton *object;
264 manager = g_dbus_object_manager_server_new(dbus_object_path);
265 gchar *s;
266 s = g_strdup_printf("%s/%s",dbus_object_path,cmd->argv[1]);
267
268 object = object_skeleton_new(s);
269 g_free(s);
270
271 FlashControl* flash_control = flash_control_skeleton_new();
272 object_skeleton_set_flash_control(object, flash_control);
273 g_object_unref(flash_control);
274
275 /* Export the object (@manager takes its own reference to @object) */
276 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
277 g_object_unref(object);
278
279 /* Export all objects */
280 g_dbus_object_manager_server_set_connection(manager, connection);
281 bool bmc_flash = false;
282 uint32_t address = 0;
283 if(strcmp(cmd->argv[1],"bmc")==0) {
284 bmc_flash = true;
285 }
286 if(strcmp(cmd->argv[1],"bmc_ramdisk")==0) {
287 bmc_flash = true;
288 address = 0x20300000;
289 }
290 if(strcmp(cmd->argv[1],"bmc_kernel")==0) {
291 bmc_flash = true;
292 address = 0x20080000;
293 }
294
295 int rc = flash(flash_control,bmc_flash,address,cmd->argv[2],cmd->argv[3]);
296 if(rc) {
297 flash_message(connection,cmd->argv[3],"error","Flash Error");
298 } else {
299 flash_message(connection,cmd->argv[3],"done","");
300 }
301
302 //Object exits when done flashing
303 g_main_loop_quit(cmd->loop);
304}
305
306int
307main(int argc, char *argv[])
308{
309 GMainLoop *loop;
310 cmdline cmd;
311 cmd.argc = argc;
312 cmd.argv = argv;
313
314 guint id;
315 loop = g_main_loop_new(NULL, FALSE);
316 cmd.loop = loop;
317
318 id = g_bus_own_name(DBUS_TYPE,
319 dbus_name,
320 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
321 G_BUS_NAME_OWNER_FLAGS_REPLACE,
322 on_bus_acquired,
323 NULL,
324 NULL,
325 &cmd,
326 NULL);
327
328 g_main_loop_run(loop);
329
330 g_bus_unown_name(id);
331 g_main_loop_unref(loop);
332
333 return 0;
334}