blob: 319cd84c30c325b9dbd3eefa60d08db6178aae6f [file] [log] [blame]
Norman James18998182015-10-11 21:54:53 -05001#include <stdio.h>
2#include <stdbool.h>
3#include <string.h>
Norman James362a80f2015-09-14 14:04:39 -05004#include "interfaces/openbmc_intf.h"
Norman James10ff6a32015-08-27 14:24:17 -05005#include "openbmc.h"
Norman Jamescfc2b442015-10-31 17:31:46 -05006#include "object_mapper.h"
Norman Jamese2765102015-08-19 22:00:55 -05007
8/* ---------------------------------------------------------------------------------------------------- */
Norman James166acf42015-10-22 07:11:51 -05009static const gchar* dbus_object_path = "/org/openbmc/control/flash";
10static const gchar* dbus_name = "org.openbmc.control.Flash";
Norman James18998182015-10-11 21:54:53 -050011static const gchar* FLASHER_BIN = "flasher.exe";
12static const gchar* DLOAD_BUS = "org.openbmc.managers.Download";
13static const gchar* DLOAD_OBJ = "/org/openbmc/managers/Download";
Norman Jamese2765102015-08-19 22:00:55 -050014
15static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050016
Norman Jamesc8719dd2015-11-01 07:38:02 -060017void catch_child(int sig_num)
18{
19 /* when we get here, we know there's a zombie child waiting */
20 int child_status;
21
22 wait(&child_status);
23 printf("flasher exited.\n");
24}
25
26
Norman James166acf42015-10-22 07:11:51 -050027int update(Flash* flash, const char* obj_path)
Norman Jamesf066e872015-10-07 15:29:51 -050028{
Norman James18998182015-10-11 21:54:53 -050029 pid_t pid;
30 int status=-1;
31 pid = fork();
32 if (pid == 0)
33 {
34 const gchar* path = flash_get_flasher_path(flash);
35 const gchar* name = flash_get_flasher_name(flash);
36 const gchar* inst = flash_get_flasher_instance(flash);
37 const gchar* filename = flash_get_filename(flash);
Norman James166acf42015-10-22 07:11:51 -050038 status = execl(path, name, inst, filename, obj_path, NULL);
Norman Jamesa3e47c42015-10-18 14:43:10 -050039 return status;
Norman Jamesf066e872015-10-07 15:29:51 -050040 }
Norman Jamesa3e47c42015-10-18 14:43:10 -050041 return 0;
Norman Jamesf066e872015-10-07 15:29:51 -050042}
43
Norman Jamese2765102015-08-19 22:00:55 -050044static gboolean
Norman James471ab592015-08-30 22:29:40 -050045on_init (Flash *f,
46 GDBusMethodInvocation *invocation,
47 gpointer user_data)
48{
Norman James65a295a2015-09-26 22:21:10 -050049 flash_complete_init(f,invocation);
Norman James068efb32015-10-06 16:52:28 -050050
Norman James18998182015-10-11 21:54:53 -050051 //tune flash
Norman James166acf42015-10-22 07:11:51 -050052 if (strcmp(flash_get_flasher_instance(f),"bios") == 0)
Norman James18998182015-10-11 21:54:53 -050053 {
Norman James166acf42015-10-22 07:11:51 -050054 flash_set_filename(f,"");
55 const gchar* obj_path = g_dbus_object_get_object_path((GDBusObject*)user_data);
56 int rc = update(f,obj_path);
57 if (rc==-1)
58 {
59 printf("ERROR FlashControl: Unable to init\n");
60 }
Norman Jamesd1f41c72015-11-01 20:30:13 -060061 sleep(3);
62 rc = update(f,obj_path);
63
Norman James18998182015-10-11 21:54:53 -050064 }
65 return TRUE;
66}
Norman James068efb32015-10-06 16:52:28 -050067
Norman James18998182015-10-11 21:54:53 -050068static gboolean
69on_lock (SharedResource *lock,
70 GDBusMethodInvocation *invocation,
71 gchar* name,
72 gpointer user_data)
73{
Norman Jamesa3e47c42015-10-18 14:43:10 -050074 gboolean locked = shared_resource_get_lock(lock);
75 if (locked)
76 {
77 const gchar* name = shared_resource_get_name(lock);
78 printf("ERROR: BIOS Flash is already locked: %s\n",name);
79 }
80 else
81 {
82 printf("Locking BIOS Flash: %s\n",name);
83 shared_resource_set_lock(lock,true);
84 shared_resource_set_name(lock,name);
85 }
Norman James18998182015-10-11 21:54:53 -050086 shared_resource_complete_lock(lock,invocation);
87 return TRUE;
88}
89static gboolean
90on_is_locked (SharedResource *lock,
91 GDBusMethodInvocation *invocation,
92 gpointer user_data)
93{
94 gboolean locked = shared_resource_get_lock(lock);
95 const gchar* name = shared_resource_get_name(lock);
96 shared_resource_complete_is_locked(lock,invocation,locked,name);
97 return TRUE;
98}
99
100
101static gboolean
102on_unlock (SharedResource *lock,
103 GDBusMethodInvocation *invocation,
104 gpointer user_data)
105{
106 printf("Unlocking BIOS Flash\n");
107 shared_resource_set_lock(lock,false);
108 shared_resource_set_name(lock,"");
109 shared_resource_complete_unlock(lock,invocation);
Norman James65a295a2015-09-26 22:21:10 -0500110 return TRUE;
Norman James471ab592015-08-30 22:29:40 -0500111}
112
113static gboolean
Norman Jamesf066e872015-10-07 15:29:51 -0500114on_update_via_tftp (Flash *flash,
115 GDBusMethodInvocation *invocation,
116 gchar* url,
117 gchar* write_file,
118 gpointer user_data)
119{
Norman James18998182015-10-11 21:54:53 -0500120 SharedResource *lock = object_get_shared_resource((Object*)user_data);
121 gboolean locked = shared_resource_get_lock(lock);
Norman Jamesf066e872015-10-07 15:29:51 -0500122 flash_complete_update_via_tftp(flash,invocation);
Norman James18998182015-10-11 21:54:53 -0500123 if (locked)
124 {
125 const gchar* name = shared_resource_get_name(lock);
126 printf("BIOS Flash is locked: %s\n",name);
127 }
128 else
129 {
130 printf("Flashing BIOS from TFTP: %s,%s\n",url,write_file);
Norman James18998182015-10-11 21:54:53 -0500131 flash_set_filename(flash,write_file);
132 flash_emit_download(flash,url,write_file);
Norman James166acf42015-10-22 07:11:51 -0500133 flash_set_status(flash,"Downloading");
Norman James18998182015-10-11 21:54:53 -0500134 }
Norman Jamesf066e872015-10-07 15:29:51 -0500135 return TRUE;
136}
137
138static gboolean
Norman James166acf42015-10-22 07:11:51 -0500139on_error (Flash *flash,
140 GDBusMethodInvocation *invocation,
141 gchar* error_msg,
142 gpointer user_data)
143{
144 int rc = 0;
145 SharedResource *lock = object_get_shared_resource((Object*)user_data);
146 gboolean locked = shared_resource_get_lock(lock);
147 flash_set_status(flash, error_msg);
148 flash_complete_error(flash,invocation);
149 printf("ERROR: %s. Clearing locks\n",error_msg);
150 shared_resource_set_lock(lock,false);
151 shared_resource_set_name(lock,"");
152
153 return TRUE;
154}
155static gboolean
156on_done (Flash *flash,
157 GDBusMethodInvocation *invocation,
158 gpointer user_data)
159{
160 int rc = 0;
161 SharedResource *lock = object_get_shared_resource((Object*)user_data);
162 gboolean locked = shared_resource_get_lock(lock);
163 flash_set_status(flash, "Flash Done");
164 flash_complete_done(flash,invocation);
165 printf("Flash Done. Clearing locks\n");
166 shared_resource_set_lock(lock,false);
167 shared_resource_set_name(lock,"");
168
169 return TRUE;
170}
171
172
173static gboolean
Norman Jamesf066e872015-10-07 15:29:51 -0500174on_update (Flash *flash,
Norman Jamese2765102015-08-19 22:00:55 -0500175 GDBusMethodInvocation *invocation,
176 gchar* write_file,
177 gpointer user_data)
178{
Norman James18998182015-10-11 21:54:53 -0500179 int rc = 0;
180 SharedResource *lock = object_get_shared_resource((Object*)user_data);
181 gboolean locked = shared_resource_get_lock(lock);
Norman James166acf42015-10-22 07:11:51 -0500182 flash_set_status(flash,"Flashing");
Norman Jamesf066e872015-10-07 15:29:51 -0500183 flash_complete_update(flash,invocation);
Norman James18998182015-10-11 21:54:53 -0500184 if (locked)
185 {
186 const gchar* name = shared_resource_get_name(lock);
187 printf("BIOS Flash is locked: %s\n",name);
188 }
189 else
190 {
191 printf("Flashing BIOS from: %s\n",write_file);
Norman James166acf42015-10-22 07:11:51 -0500192 flash_set_status(flash, "Flashing");
Norman James18998182015-10-11 21:54:53 -0500193 shared_resource_set_lock(lock,true);
194 shared_resource_set_name(lock,dbus_object_path);
195 flash_set_filename(flash,write_file);
Norman James166acf42015-10-22 07:11:51 -0500196 const gchar* obj_path = g_dbus_object_get_object_path((GDBusObject*)user_data);
197 rc = update(flash,obj_path);
Norman James18998182015-10-11 21:54:53 -0500198 if (!rc)
199 {
200 shared_resource_set_lock(lock,false);
201 shared_resource_set_name(lock,"");
202 }
203 }
Norman James068efb32015-10-06 16:52:28 -0500204 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -0500205}
Norman James18998182015-10-11 21:54:53 -0500206static void
207on_flash_progress (GDBusConnection* connection,
208 const gchar* sender_name,
209 const gchar* object_path,
210 const gchar* interface_name,
211 const gchar* signal_name,
212 GVariant* parameters,
213 gpointer user_data)
214{
215 Flash *flash = object_get_flash((Object*)user_data);
216 SharedResource *lock = object_get_shared_resource((Object*)user_data);
217 GVariantIter *iter = g_variant_iter_new(parameters);
218 GVariant* v_filename = g_variant_iter_next_value(iter);
219 GVariant* v_progress = g_variant_iter_next_value(iter);
Norman Jamesf066e872015-10-07 15:29:51 -0500220
Norman James18998182015-10-11 21:54:53 -0500221 uint8_t progress = g_variant_get_byte(v_progress);
Norman James166acf42015-10-22 07:11:51 -0500222
223 gchar *s;
224 s = g_strdup_printf ("Flashing: %d%%",progress);
225 flash_set_status(flash,s);
226 g_free(s);
Norman James18998182015-10-11 21:54:53 -0500227}
228
229static void
230on_flash_done (GDBusConnection* connection,
231 const gchar* sender_name,
232 const gchar* object_path,
233 const gchar* interface_name,
234 const gchar* signal_name,
235 GVariant* parameters,
236 gpointer user_data)
237{
238 Flash *flash = object_get_flash((Object*)user_data);
239 SharedResource *lock = object_get_shared_resource((Object*)user_data);
240 printf("Flash succeeded; unlocking flash\n");
241 shared_resource_set_lock(lock,false);
242 shared_resource_set_name(lock,"");
Norman James166acf42015-10-22 07:11:51 -0500243 flash_set_status(flash,"Flash Done");
Norman James18998182015-10-11 21:54:53 -0500244}
245
246static void
247on_flash_error (GDBusConnection* connection,
248 const gchar* sender_name,
249 const gchar* object_path,
250 const gchar* interface_name,
251 const gchar* signal_name,
252 GVariant* parameters,
253 gpointer user_data)
254{
255 Flash *flash = object_get_flash((Object*)user_data);
256 SharedResource *lock = object_get_shared_resource((Object*)user_data);
257 printf("Flash Error; unlocking flash\n");
258 shared_resource_set_lock(lock,false);
259 shared_resource_set_name(lock,"");
260}
261
262static void
263on_download_error (GDBusConnection* connection,
264 const gchar* sender_name,
265 const gchar* object_path,
266 const gchar* interface_name,
267 const gchar* signal_name,
268 GVariant* parameters,
269 gpointer user_data)
270{
271 Flash *flash = object_get_flash((Object*)user_data);
272 SharedResource *lock = object_get_shared_resource((Object*)user_data);
273 printf("ERROR: FlashBios: Download error; clearing flash lock\n");
274 shared_resource_set_lock(lock,false);
275 shared_resource_set_name(lock,"");
Norman Jamesf066e872015-10-07 15:29:51 -0500276}
277
Norman Jamese2765102015-08-19 22:00:55 -0500278static void
279on_bus_acquired (GDBusConnection *connection,
280 const gchar *name,
281 gpointer user_data)
282{
Norman James10ff6a32015-08-27 14:24:17 -0500283 ObjectSkeleton *object;
Norman James10ff6a32015-08-27 14:24:17 -0500284 cmdline *cmd = user_data;
Norman James10ff6a32015-08-27 14:24:17 -0500285 manager = g_dbus_object_manager_server_new (dbus_object_path);
286 int i=0;
Norman James166acf42015-10-22 07:11:51 -0500287
288 //TODO: don't use fixed buffer
289 char flasher_path[512];
290 memset(flasher_path, '\0', sizeof(flasher_path));
291 bool found = false;
292 gchar *flasher_file;
293 int c = strlen(cmd->argv[0]);
294 while(c>0)
295 {
296 if (cmd->argv[0][c] == '/')
297 {
298 strncpy(flasher_path,cmd->argv[0],c);
299 flasher_file = g_strdup_printf ("%s/%s",flasher_path,FLASHER_BIN);
300 break;
301 }
302 c--;
303 }
304
Norman James15aaa802015-10-28 06:54:48 -0500305 const char* inst[] = {"bios","bmc","bmc_ramdisk","bmc_kernel"};
306 for (i=0;i<4;i++)
Norman James10ff6a32015-08-27 14:24:17 -0500307 {
Norman James166acf42015-10-22 07:11:51 -0500308 gchar* s;
309 s = g_strdup_printf ("%s/%s",dbus_object_path,inst[i]);
Norman James10ff6a32015-08-27 14:24:17 -0500310 object = object_skeleton_new (s);
311 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -0500312
Norman James10ff6a32015-08-27 14:24:17 -0500313 Flash* flash = flash_skeleton_new ();
314 object_skeleton_set_flash (object, flash);
315 g_object_unref (flash);
Norman Jamese2765102015-08-19 22:00:55 -0500316
Norman James18998182015-10-11 21:54:53 -0500317 SharedResource* lock = shared_resource_skeleton_new ();
318 object_skeleton_set_shared_resource (object, lock);
319 g_object_unref (lock);
320
Norman Jamescfc2b442015-10-31 17:31:46 -0500321 ObjectMapper* mapper = object_mapper_skeleton_new ();
322 object_skeleton_set_object_mapper (object, mapper);
323 g_object_unref (mapper);
324
Norman James18998182015-10-11 21:54:53 -0500325 shared_resource_set_lock(lock,false);
326 shared_resource_set_name(lock,"");
327
Norman James166acf42015-10-22 07:11:51 -0500328 flash_set_flasher_path(flash,flasher_file);
329 flash_set_flasher_name(flash,FLASHER_BIN);
330 flash_set_flasher_instance(flash,inst[i]);
331 //g_free (s);
Norman James18998182015-10-11 21:54:53 -0500332
333
Norman James10ff6a32015-08-27 14:24:17 -0500334 //define method callbacks here
Norman James18998182015-10-11 21:54:53 -0500335 g_signal_connect (lock,
336 "handle-lock",
337 G_CALLBACK (on_lock),
338 NULL); /* user_data */
339 g_signal_connect (lock,
340 "handle-unlock",
341 G_CALLBACK (on_unlock),
342 NULL); /* user_data */
343 g_signal_connect (lock,
344 "handle-is-locked",
345 G_CALLBACK (on_is_locked),
346 NULL); /* user_data */
347
Norman James10ff6a32015-08-27 14:24:17 -0500348 g_signal_connect (flash,
Norman James8c6d8382015-10-06 07:47:16 -0500349 "handle-update",
350 G_CALLBACK (on_update),
Norman James18998182015-10-11 21:54:53 -0500351 object); /* user_data */
Norman James166acf42015-10-22 07:11:51 -0500352
353 g_signal_connect (flash,
354 "handle-error",
355 G_CALLBACK (on_error),
356 object); /* user_data */
357
358 g_signal_connect (flash,
359 "handle-done",
360 G_CALLBACK (on_done),
361 object); /* user_data */
362
Norman Jamesf066e872015-10-07 15:29:51 -0500363 g_signal_connect (flash,
364 "handle-update-via-tftp",
365 G_CALLBACK (on_update_via_tftp),
Norman James18998182015-10-11 21:54:53 -0500366 object); /* user_data */
Norman Jamesf066e872015-10-07 15:29:51 -0500367
Norman James471ab592015-08-30 22:29:40 -0500368 g_signal_connect (flash,
369 "handle-init",
370 G_CALLBACK (on_init),
Norman James166acf42015-10-22 07:11:51 -0500371 object); /* user_data */
Norman James471ab592015-08-30 22:29:40 -0500372
Norman James166acf42015-10-22 07:11:51 -0500373 s = g_strdup_printf ("/org/openbmc/control/%s",inst[i]);
Norman James18998182015-10-11 21:54:53 -0500374 g_dbus_connection_signal_subscribe(connection,
375 NULL,
376 "org.openbmc.FlashControl",
377 "Progress",
378 s,
379 NULL,
380 G_DBUS_SIGNAL_FLAGS_NONE,
381 (GDBusSignalCallback) on_flash_progress,
382 object,
383 NULL );
384
385 g_free (s);
Norman Jamesf066e872015-10-07 15:29:51 -0500386
Norman James471ab592015-08-30 22:29:40 -0500387
Norman James18998182015-10-11 21:54:53 -0500388 flash_set_filename(flash,"");
Norman James10ff6a32015-08-27 14:24:17 -0500389 /* Export the object (@manager takes its own reference to @object) */
390 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
391 g_object_unref (object);
392 }
Norman James166acf42015-10-22 07:11:51 -0500393 g_free(flasher_file);
Norman James10ff6a32015-08-27 14:24:17 -0500394 /* Export all objects */
395 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamescfc2b442015-10-31 17:31:46 -0500396 emit_object_added((GDBusObjectManager*)manager);
Norman Jamese2765102015-08-19 22:00:55 -0500397}
398
399static void
400on_name_acquired (GDBusConnection *connection,
401 const gchar *name,
402 gpointer user_data)
403{
Norman James362a80f2015-09-14 14:04:39 -0500404// g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500405}
406
407static void
408on_name_lost (GDBusConnection *connection,
409 const gchar *name,
410 gpointer user_data)
411{
Norman James362a80f2015-09-14 14:04:39 -0500412 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500413}
414
415gint
416main (gint argc, gchar *argv[])
417{
418 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500419 cmdline cmd;
420 cmd.argc = argc;
421 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500422 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500423 loop = g_main_loop_new (NULL, FALSE);
424
Norman Jamesd1f41c72015-11-01 20:30:13 -0600425 //signal(SIGCHLD, catch_child);
Norman James5e792e32015-10-07 17:36:17 -0500426 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500427 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500428 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
429 G_BUS_NAME_OWNER_FLAGS_REPLACE,
430 on_bus_acquired,
431 on_name_acquired,
432 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500433 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500434 NULL);
435
436 g_main_loop_run (loop);
437
438 g_bus_unown_name (id);
439 g_main_loop_unref (loop);
440 return 0;
441}