blob: 4c407080319b9d45f5c0e9ae2a57957f341fc65f [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{
Norman James65a295a2015-09-26 22:21:10 -050016 flash_complete_init(f,invocation);
17 //tune flash
18 g_print("Tuning BIOS Flash\n");
19 flash_access_setup_pnor(true, false, false);
20 return TRUE;
Norman James471ab592015-08-30 22:29:40 -050021}
22
23static gboolean
Norman Jamese2765102015-08-19 22:00:55 -050024on_update_via_file (Flash *f,
25 GDBusMethodInvocation *invocation,
26 gchar* write_file,
27 gpointer user_data)
28{
29 g_print("Flashing BIOS from file\n");
30 // get size from file
31 struct stat stbuf;
32 uint32_t address = 0, read_size = 0, write_size = 0;
33
34 if (stat(write_file, &stbuf))
35 {
36 g_print("Failed to get file size");
37 //TODO: Error handling
38 }
39 write_size = stbuf.st_size;
40 erase_chip();
41 program_file(write_file, address, write_size);
42 flash_complete_update_via_file(f,invocation);
43 return TRUE;
44}
45
46static void
47on_bus_acquired (GDBusConnection *connection,
48 const gchar *name,
49 gpointer user_data)
50{
Norman James10ff6a32015-08-27 14:24:17 -050051 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050052// g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050053 cmdline *cmd = user_data;
54 if (cmd->argc < 2)
55 {
56 g_print("No objects created. Put object name(s) on command line\n");
57 return;
58 }
59 manager = g_dbus_object_manager_server_new (dbus_object_path);
60 int i=0;
61 for (i=1;i<cmd->argc;i++)
62 {
63 gchar *s;
64 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
65 object = object_skeleton_new (s);
66 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050067
Norman James10ff6a32015-08-27 14:24:17 -050068 Flash* flash = flash_skeleton_new ();
69 object_skeleton_set_flash (object, flash);
70 g_object_unref (flash);
Norman Jamese2765102015-08-19 22:00:55 -050071
Norman James10ff6a32015-08-27 14:24:17 -050072 //define method callbacks here
73 g_signal_connect (flash,
Norman Jamese2765102015-08-19 22:00:55 -050074 "handle-update-via-file",
75 G_CALLBACK (on_update_via_file),
76 NULL); /* user_data */
Norman James471ab592015-08-30 22:29:40 -050077
78 g_signal_connect (flash,
79 "handle-init",
80 G_CALLBACK (on_init),
81 NULL); /* user_data */
82
83
Norman James10ff6a32015-08-27 14:24:17 -050084 /* Export the object (@manager takes its own reference to @object) */
85 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
86 g_object_unref (object);
87 }
Norman Jamese2765102015-08-19 22:00:55 -050088
Norman James10ff6a32015-08-27 14:24:17 -050089 /* Export all objects */
90 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamese2765102015-08-19 22:00:55 -050091}
92
93static void
94on_name_acquired (GDBusConnection *connection,
95 const gchar *name,
96 gpointer user_data)
97{
Norman James362a80f2015-09-14 14:04:39 -050098// g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -050099}
100
101static void
102on_name_lost (GDBusConnection *connection,
103 const gchar *name,
104 gpointer user_data)
105{
Norman James362a80f2015-09-14 14:04:39 -0500106 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500107}
108
109gint
110main (gint argc, gchar *argv[])
111{
112 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500113 cmdline cmd;
114 cmd.argc = argc;
115 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500116
117 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500118 loop = g_main_loop_new (NULL, FALSE);
119
120 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500121 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500122 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
123 G_BUS_NAME_OWNER_FLAGS_REPLACE,
124 on_bus_acquired,
125 on_name_acquired,
126 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500127 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500128 NULL);
129
130 g_main_loop_run (loop);
131
132 g_bus_unown_name (id);
133 g_main_loop_unref (loop);
134 return 0;
135}