blob: bc5c0706fedfa1537a2aefb7d58746be47e6f273 [file] [log] [blame]
Norman Jamese2765102015-08-19 22:00:55 -05001#include "interfaces/flash.h"
2#include "pflash/pflash.c"
Norman James10ff6a32015-08-27 14:24:17 -05003#include "openbmc.h"
Norman Jamese2765102015-08-19 22:00:55 -05004
5/* ---------------------------------------------------------------------------------------------------- */
Norman James26072c02015-08-25 07:14:29 -05006static const gchar* dbus_object_path = "/org/openbmc/flash/BIOS";
7static const gchar* dbus_name = "org.openbmc.flash.BIOS";
Norman Jamese2765102015-08-19 22:00:55 -05008
9static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050010
11static gboolean
12on_update_via_file (Flash *f,
13 GDBusMethodInvocation *invocation,
14 gchar* write_file,
15 gpointer user_data)
16{
17 g_print("Flashing BIOS from file\n");
18 // get size from file
19 struct stat stbuf;
20 uint32_t address = 0, read_size = 0, write_size = 0;
21
22 if (stat(write_file, &stbuf))
23 {
24 g_print("Failed to get file size");
25 //TODO: Error handling
26 }
27 write_size = stbuf.st_size;
28 erase_chip();
29 program_file(write_file, address, write_size);
30 flash_complete_update_via_file(f,invocation);
31 return TRUE;
32}
33
34static void
35on_bus_acquired (GDBusConnection *connection,
36 const gchar *name,
37 gpointer user_data)
38{
Norman James10ff6a32015-08-27 14:24:17 -050039 ObjectSkeleton *object;
40 g_print ("Acquired a message bus connection: %s\n",name);
41 cmdline *cmd = user_data;
42 if (cmd->argc < 2)
43 {
44 g_print("No objects created. Put object name(s) on command line\n");
45 return;
46 }
47 manager = g_dbus_object_manager_server_new (dbus_object_path);
48 int i=0;
49 for (i=1;i<cmd->argc;i++)
50 {
51 gchar *s;
52 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
53 object = object_skeleton_new (s);
54 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050055
Norman James10ff6a32015-08-27 14:24:17 -050056 Flash* flash = flash_skeleton_new ();
57 object_skeleton_set_flash (object, flash);
58 g_object_unref (flash);
Norman Jamese2765102015-08-19 22:00:55 -050059
Norman James10ff6a32015-08-27 14:24:17 -050060 //define method callbacks here
61 g_signal_connect (flash,
Norman Jamese2765102015-08-19 22:00:55 -050062 "handle-update-via-file",
63 G_CALLBACK (on_update_via_file),
64 NULL); /* user_data */
65
Norman James10ff6a32015-08-27 14:24:17 -050066 /* Export the object (@manager takes its own reference to @object) */
67 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
68 g_object_unref (object);
69 }
Norman Jamese2765102015-08-19 22:00:55 -050070
Norman James10ff6a32015-08-27 14:24:17 -050071 /* Export all objects */
72 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamese2765102015-08-19 22:00:55 -050073}
74
75static void
76on_name_acquired (GDBusConnection *connection,
77 const gchar *name,
78 gpointer user_data)
79{
80 g_print ("Acquired the name %s\n", name);
81}
82
83static void
84on_name_lost (GDBusConnection *connection,
85 const gchar *name,
86 gpointer user_data)
87{
88 g_print ("Lost the name %s\n", name);
89}
90
91gint
92main (gint argc, gchar *argv[])
93{
94 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -050095 cmdline cmd;
96 cmd.argc = argc;
97 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -050098
99 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500100 loop = g_main_loop_new (NULL, FALSE);
101
102 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500103 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500104 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
105 G_BUS_NAME_OWNER_FLAGS_REPLACE,
106 on_bus_acquired,
107 on_name_acquired,
108 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500109 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500110 NULL);
111
112 g_main_loop_run (loop);
113
114 g_bus_unown_name (id);
115 g_main_loop_unref (loop);
116 return 0;
117}