blob: bb95d172f02a05707f04ef6c872e39c94758fb16 [file] [log] [blame]
Norman James362a80f2015-09-14 14:04:39 -05001#include "interfaces/openbmc_intf.h"
Norman Jamese2765102015-08-19 22:00:55 -05002#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 James362a80f2015-09-14 14:04:39 -05006static const gchar* dbus_object_path = "/org/openbmc/flash";
Norman James26072c02015-08-25 07:14:29 -05007static 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
Norman James471ab592015-08-30 22:29:40 -050012on_init (Flash *f,
13 GDBusMethodInvocation *invocation,
14 gpointer user_data)
15{
16 flash_complete_init(f,invocation);
17 return TRUE;
18
19}
20
21static gboolean
Norman Jamese2765102015-08-19 22:00:55 -050022on_update_via_file (Flash *f,
23 GDBusMethodInvocation *invocation,
24 gchar* write_file,
25 gpointer user_data)
26{
27 g_print("Flashing BIOS from file\n");
28 // get size from file
29 struct stat stbuf;
30 uint32_t address = 0, read_size = 0, write_size = 0;
31
32 if (stat(write_file, &stbuf))
33 {
34 g_print("Failed to get file size");
35 //TODO: Error handling
36 }
37 write_size = stbuf.st_size;
38 erase_chip();
39 program_file(write_file, address, write_size);
40 flash_complete_update_via_file(f,invocation);
41 return TRUE;
42}
43
44static void
45on_bus_acquired (GDBusConnection *connection,
46 const gchar *name,
47 gpointer user_data)
48{
Norman James10ff6a32015-08-27 14:24:17 -050049 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050050// g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050051 cmdline *cmd = user_data;
52 if (cmd->argc < 2)
53 {
54 g_print("No objects created. Put object name(s) on command line\n");
55 return;
56 }
57 manager = g_dbus_object_manager_server_new (dbus_object_path);
58 int i=0;
59 for (i=1;i<cmd->argc;i++)
60 {
61 gchar *s;
62 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
63 object = object_skeleton_new (s);
64 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050065
Norman James10ff6a32015-08-27 14:24:17 -050066 Flash* flash = flash_skeleton_new ();
67 object_skeleton_set_flash (object, flash);
68 g_object_unref (flash);
Norman Jamese2765102015-08-19 22:00:55 -050069
Norman James10ff6a32015-08-27 14:24:17 -050070 //define method callbacks here
71 g_signal_connect (flash,
Norman Jamese2765102015-08-19 22:00:55 -050072 "handle-update-via-file",
73 G_CALLBACK (on_update_via_file),
74 NULL); /* user_data */
Norman James471ab592015-08-30 22:29:40 -050075
76 g_signal_connect (flash,
77 "handle-init",
78 G_CALLBACK (on_init),
79 NULL); /* user_data */
80
81
Norman James10ff6a32015-08-27 14:24:17 -050082 /* Export the object (@manager takes its own reference to @object) */
83 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
84 g_object_unref (object);
85 }
Norman Jamese2765102015-08-19 22:00:55 -050086
Norman James10ff6a32015-08-27 14:24:17 -050087 /* Export all objects */
88 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamese2765102015-08-19 22:00:55 -050089}
90
91static void
92on_name_acquired (GDBusConnection *connection,
93 const gchar *name,
94 gpointer user_data)
95{
Norman James362a80f2015-09-14 14:04:39 -050096// g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -050097}
98
99static void
100on_name_lost (GDBusConnection *connection,
101 const gchar *name,
102 gpointer user_data)
103{
Norman James362a80f2015-09-14 14:04:39 -0500104 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500105}
106
107gint
108main (gint argc, gchar *argv[])
109{
110 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500111 cmdline cmd;
112 cmd.argc = argc;
113 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500114
115 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500116 loop = g_main_loop_new (NULL, FALSE);
117
118 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500119 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500120 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
121 G_BUS_NAME_OWNER_FLAGS_REPLACE,
122 on_bus_acquired,
123 on_name_acquired,
124 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500125 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500126 NULL);
127
128 g_main_loop_run (loop);
129
130 g_bus_unown_name (id);
131 g_main_loop_unref (loop);
132 return 0;
133}