blob: 3a4ae1fdea55ba83f7ded735f3a0d43277fd292a [file] [log] [blame]
Norman Jamese2765102015-08-19 22:00:55 -05001#include "interfaces/flash.h"
2#include "pflash/pflash.c"
3
4/* ---------------------------------------------------------------------------------------------------- */
Norman James26072c02015-08-25 07:14:29 -05005static const gchar* dbus_object_path = "/org/openbmc/flash/BIOS";
6static const gchar* dbus_name = "org.openbmc.flash.BIOS";
Norman Jamese2765102015-08-19 22:00:55 -05007
8static GDBusObjectManagerServer *manager = NULL;
9static Flash *flash = NULL;
10
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{
39 ObjectSkeleton *object;
40 guint n;
41
42 g_print ("Acquired a message bus connection: %s\n",name);
43
Norman James26072c02015-08-25 07:14:29 -050044 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman Jamese2765102015-08-19 22:00:55 -050045
46 gchar *s;
Norman James26072c02015-08-25 07:14:29 -050047 s = g_strdup_printf ("%s/0",dbus_object_path);
Norman Jamese2765102015-08-19 22:00:55 -050048 object = object_skeleton_new (s);
49 g_free (s);
50
51 flash = flash_skeleton_new ();
52 object_skeleton_set_flash (object, flash);
53 g_object_unref (flash);
54
55 //define method callbacks here
56 g_signal_connect (flash,
57 "handle-update-via-file",
58 G_CALLBACK (on_update_via_file),
59 NULL); /* user_data */
60
61 /* Export the object (@manager takes its own reference to @object) */
62 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
63 g_object_unref (object);
64
65 /* Export all objects */
66 g_dbus_object_manager_server_set_connection (manager, connection);
67}
68
69static void
70on_name_acquired (GDBusConnection *connection,
71 const gchar *name,
72 gpointer user_data)
73{
74 g_print ("Acquired the name %s\n", name);
75}
76
77static void
78on_name_lost (GDBusConnection *connection,
79 const gchar *name,
80 gpointer user_data)
81{
82 g_print ("Lost the name %s\n", name);
83}
84
85gint
86main (gint argc, gchar *argv[])
87{
88 GMainLoop *loop;
89
90 guint id;
91 //g_type_init ();
92 loop = g_main_loop_new (NULL, FALSE);
93
94 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -050095 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -050096 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
97 G_BUS_NAME_OWNER_FLAGS_REPLACE,
98 on_bus_acquired,
99 on_name_acquired,
100 on_name_lost,
101 loop,
102 NULL);
103
104 g_main_loop_run (loop);
105
106 g_bus_unown_name (id);
107 g_main_loop_unref (loop);
108 return 0;
109}