blob: cb3f70af4522477c629e412be9ef2cd1f41b4c9f [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
Norman Jamesf066e872015-10-07 15:29:51 -050011void update(Flash *flash, const gchar* write_file)
12{
13 printf("Flashing: %s\n",write_file);
14 // get size from file
15 struct stat stbuf;
16 uint32_t address = 0, read_size = 0, write_size = 0;
17
18#ifdef __arm__
19 if (stat(write_file, &stbuf))
20 {
21 printf("ERROR: Invalid flash file: %s\n",write_file);
22 }
23 write_size = stbuf.st_size;
24 // TODO: need to change pflash to return error instead of exit
25 erase_chip();
26 program_file(write_file, address, write_size);
27#endif
28 flash_emit_updated(flash);
29}
30
Norman Jamese2765102015-08-19 22:00:55 -050031static gboolean
Norman James471ab592015-08-30 22:29:40 -050032on_init (Flash *f,
33 GDBusMethodInvocation *invocation,
34 gpointer user_data)
35{
Norman James65a295a2015-09-26 22:21:10 -050036 flash_complete_init(f,invocation);
Norman James068efb32015-10-06 16:52:28 -050037
38 #ifdef __arm__
39 printf("Tuning BIOS Flash\n");
40 flash_access_setup_pnor(true, false, false);
41 #endif
42
Norman James65a295a2015-09-26 22:21:10 -050043 return TRUE;
Norman James471ab592015-08-30 22:29:40 -050044}
45
46static gboolean
Norman Jamesf066e872015-10-07 15:29:51 -050047on_update_via_tftp (Flash *flash,
48 GDBusMethodInvocation *invocation,
49 gchar* url,
50 gchar* write_file,
51 gpointer user_data)
52{
53 printf("Flashing BIOS from TFTP: %s,%s\n",url,write_file);
54 flash_emit_download(flash,url,write_file);
55 flash_complete_update_via_tftp(flash,invocation);
56 return TRUE;
57}
58
59static gboolean
60on_update (Flash *flash,
Norman Jamese2765102015-08-19 22:00:55 -050061 GDBusMethodInvocation *invocation,
62 gchar* write_file,
63 gpointer user_data)
64{
Norman James068efb32015-10-06 16:52:28 -050065 printf("Flashing BIOS from file\n");
Norman Jamesf066e872015-10-07 15:29:51 -050066 flash_complete_update(flash,invocation);
67 update(flash,write_file);
68 flash_emit_updated(flash);
Norman James068efb32015-10-06 16:52:28 -050069 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050070}
71
Norman Jamesf066e872015-10-07 15:29:51 -050072static void
73on_download_complete (GDBusConnection* connection,
74 const gchar* sender_name,
75 const gchar* object_path,
76 const gchar* interface_name,
77 const gchar* signal_name,
78 GVariant* parameters,
79 gpointer user_data)
80{
81 Flash *flash = object_get_flash((Object*)user_data);
82
83 GVariantIter *iter = g_variant_iter_new(parameters);
84 GVariant* value = g_variant_iter_next_value(iter);
85 const gchar* write_file;
86 gsize size;
87 write_file = g_variant_get_string(value,&size);
88 update(flash,write_file);
89
90}
91
Norman Jamese2765102015-08-19 22:00:55 -050092static void
93on_bus_acquired (GDBusConnection *connection,
94 const gchar *name,
95 gpointer user_data)
96{
Norman James10ff6a32015-08-27 14:24:17 -050097 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050098// g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050099 cmdline *cmd = user_data;
100 if (cmd->argc < 2)
101 {
102 g_print("No objects created. Put object name(s) on command line\n");
103 return;
104 }
105 manager = g_dbus_object_manager_server_new (dbus_object_path);
106 int i=0;
107 for (i=1;i<cmd->argc;i++)
108 {
109 gchar *s;
110 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
111 object = object_skeleton_new (s);
112 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -0500113
Norman James10ff6a32015-08-27 14:24:17 -0500114 Flash* flash = flash_skeleton_new ();
115 object_skeleton_set_flash (object, flash);
116 g_object_unref (flash);
Norman Jamese2765102015-08-19 22:00:55 -0500117
Norman James10ff6a32015-08-27 14:24:17 -0500118 //define method callbacks here
119 g_signal_connect (flash,
Norman James8c6d8382015-10-06 07:47:16 -0500120 "handle-update",
121 G_CALLBACK (on_update),
Norman Jamese2765102015-08-19 22:00:55 -0500122 NULL); /* user_data */
Norman Jamesf066e872015-10-07 15:29:51 -0500123 g_signal_connect (flash,
124 "handle-update-via-tftp",
125 G_CALLBACK (on_update_via_tftp),
126 NULL); /* user_data */
127
Norman James471ab592015-08-30 22:29:40 -0500128 g_signal_connect (flash,
129 "handle-init",
130 G_CALLBACK (on_init),
131 NULL); /* user_data */
132
Norman Jamesf066e872015-10-07 15:29:51 -0500133 g_dbus_connection_signal_subscribe(connection,
134 "org.openbmc.managers.Download",
135 "org.openbmc.managers.Download",
136 "DownloadComplete",
137 "/org/openbmc/managers/Download",
138 NULL,
139 G_DBUS_SIGNAL_FLAGS_NONE,
140 (GDBusSignalCallback) on_download_complete,
141 object,
142 NULL );
143
Norman James471ab592015-08-30 22:29:40 -0500144
Norman James10ff6a32015-08-27 14:24:17 -0500145 /* Export the object (@manager takes its own reference to @object) */
146 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
147 g_object_unref (object);
148 }
Norman Jamese2765102015-08-19 22:00:55 -0500149
Norman James10ff6a32015-08-27 14:24:17 -0500150 /* Export all objects */
151 g_dbus_object_manager_server_set_connection (manager, connection);
Norman Jamese2765102015-08-19 22:00:55 -0500152}
153
154static void
155on_name_acquired (GDBusConnection *connection,
156 const gchar *name,
157 gpointer user_data)
158{
Norman James362a80f2015-09-14 14:04:39 -0500159// g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500160}
161
162static void
163on_name_lost (GDBusConnection *connection,
164 const gchar *name,
165 gpointer user_data)
166{
Norman James362a80f2015-09-14 14:04:39 -0500167 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500168}
169
170gint
171main (gint argc, gchar *argv[])
172{
173 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500174 cmdline cmd;
175 cmd.argc = argc;
176 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500177
178 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500179 loop = g_main_loop_new (NULL, FALSE);
180
181 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500182 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500183 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
184 G_BUS_NAME_OWNER_FLAGS_REPLACE,
185 on_bus_acquired,
186 on_name_acquired,
187 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500188 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500189 NULL);
190
191 g_main_loop_run (loop);
192
193 g_bus_unown_name (id);
194 g_main_loop_unref (loop);
195 return 0;
196}