blob: 2a5471bf4c2558434f4079fecf2ed6272b512776 [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 James8c6d8382015-10-06 07:47:16 -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);
Norman James068efb32015-10-06 16:52:28 -050017
18 #ifdef __arm__
19 printf("Tuning BIOS Flash\n");
20 flash_access_setup_pnor(true, false, false);
21 #endif
22
Norman James65a295a2015-09-26 22:21:10 -050023 return TRUE;
Norman James471ab592015-08-30 22:29:40 -050024}
25
26static gboolean
Norman James8c6d8382015-10-06 07:47:16 -050027on_update (Flash *f,
Norman Jamese2765102015-08-19 22:00:55 -050028 GDBusMethodInvocation *invocation,
29 gchar* write_file,
30 gpointer user_data)
31{
Norman James068efb32015-10-06 16:52:28 -050032 printf("Flashing BIOS from file\n");
33 flash_complete_update(f,invocation);
34 // get size from file
35 struct stat stbuf;
36 uint32_t address = 0, read_size = 0, write_size = 0;
Norman Jamese2765102015-08-19 22:00:55 -050037
Norman James068efb32015-10-06 16:52:28 -050038#ifdef __arm__
39 if (stat(write_file, &stbuf))
40 {
41 printf("ERROR: Invalid flash file: %s\n",write_file);
42 }
43 write_size = stbuf.st_size;
44 // TODO: need to change pflash to return error instead of exit
45 erase_chip();
46 program_file(write_file, address, write_size);
47#endif
48
49 flash_emit_updated(f);
50 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050051}
52
53static void
54on_bus_acquired (GDBusConnection *connection,
55 const gchar *name,
56 gpointer user_data)
57{
Norman James10ff6a32015-08-27 14:24:17 -050058 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050059// g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050060 cmdline *cmd = user_data;
61 if (cmd->argc < 2)
62 {
63 g_print("No objects created. Put object name(s) on command line\n");
64 return;
65 }
66 manager = g_dbus_object_manager_server_new (dbus_object_path);
67 int i=0;
68 for (i=1;i<cmd->argc;i++)
69 {
70 gchar *s;
71 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
72 object = object_skeleton_new (s);
73 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050074
Norman James10ff6a32015-08-27 14:24:17 -050075 Flash* flash = flash_skeleton_new ();
76 object_skeleton_set_flash (object, flash);
77 g_object_unref (flash);
Norman Jamese2765102015-08-19 22:00:55 -050078
Norman James10ff6a32015-08-27 14:24:17 -050079 //define method callbacks here
80 g_signal_connect (flash,
Norman James8c6d8382015-10-06 07:47:16 -050081 "handle-update",
82 G_CALLBACK (on_update),
Norman Jamese2765102015-08-19 22:00:55 -050083 NULL); /* user_data */
Norman James471ab592015-08-30 22:29:40 -050084
85 g_signal_connect (flash,
86 "handle-init",
87 G_CALLBACK (on_init),
88 NULL); /* user_data */
89
90
Norman James10ff6a32015-08-27 14:24:17 -050091 /* Export the object (@manager takes its own reference to @object) */
92 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
93 g_object_unref (object);
94 }
Norman Jamese2765102015-08-19 22:00:55 -050095
Norman James10ff6a32015-08-27 14:24:17 -050096 /* Export all objects */
97 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamese2765102015-08-19 22:00:55 -050098}
99
100static void
101on_name_acquired (GDBusConnection *connection,
102 const gchar *name,
103 gpointer user_data)
104{
Norman James362a80f2015-09-14 14:04:39 -0500105// g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500106}
107
108static void
109on_name_lost (GDBusConnection *connection,
110 const gchar *name,
111 gpointer user_data)
112{
Norman James362a80f2015-09-14 14:04:39 -0500113 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500114}
115
116gint
117main (gint argc, gchar *argv[])
118{
119 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500120 cmdline cmd;
121 cmd.argc = argc;
122 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500123
124 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500125 loop = g_main_loop_new (NULL, FALSE);
126
127 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500128 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500129 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
130 G_BUS_NAME_OWNER_FLAGS_REPLACE,
131 on_bus_acquired,
132 on_name_acquired,
133 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500134 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500135 NULL);
136
137 g_main_loop_run (loop);
138
139 g_bus_unown_name (id);
140 g_main_loop_unref (loop);
141 return 0;
142}