Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 1 | #include "interfaces/openbmc_intf.h"
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 2 | #include "pflash/pflash.c"
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 3 | #include "openbmc.h"
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 4 |
|
| 5 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 6 | static const gchar* dbus_object_path = "/org/openbmc/flash";
|
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 7 | static const gchar* dbus_name = "org.openbmc.flash.Bios";
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 8 |
|
| 9 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 10 |
|
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 11 | void 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 31 | static gboolean
|
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 32 | on_init (Flash *f,
|
| 33 | GDBusMethodInvocation *invocation,
|
| 34 | gpointer user_data)
|
| 35 | {
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 36 | flash_complete_init(f,invocation);
|
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 37 |
|
| 38 | #ifdef __arm__
|
| 39 | printf("Tuning BIOS Flash\n");
|
| 40 | flash_access_setup_pnor(true, false, false);
|
| 41 | #endif
|
| 42 |
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 43 | return TRUE;
|
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 44 | }
|
| 45 |
|
| 46 | static gboolean
|
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 47 | on_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 |
|
| 59 | static gboolean
|
| 60 | on_update (Flash *flash,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 61 | GDBusMethodInvocation *invocation,
|
| 62 | gchar* write_file,
|
| 63 | gpointer user_data)
|
| 64 | {
|
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 65 | printf("Flashing BIOS from file\n");
|
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 66 | flash_complete_update(flash,invocation);
|
| 67 | update(flash,write_file);
|
| 68 | flash_emit_updated(flash);
|
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 69 | return TRUE;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 70 | }
|
| 71 |
|
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 72 | static void
|
| 73 | on_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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 92 | static void
|
| 93 | on_bus_acquired (GDBusConnection *connection,
|
| 94 | const gchar *name,
|
| 95 | gpointer user_data)
|
| 96 | {
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 97 | ObjectSkeleton *object;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 98 | // g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 99 | 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 113 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 114 | Flash* flash = flash_skeleton_new ();
|
| 115 | object_skeleton_set_flash (object, flash);
|
| 116 | g_object_unref (flash);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 117 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 118 | //define method callbacks here
|
| 119 | g_signal_connect (flash,
|
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 120 | "handle-update",
|
| 121 | G_CALLBACK (on_update),
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 122 | NULL); /* user_data */
|
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 123 | g_signal_connect (flash,
|
| 124 | "handle-update-via-tftp",
|
| 125 | G_CALLBACK (on_update_via_tftp),
|
| 126 | NULL); /* user_data */
|
| 127 |
|
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 128 | g_signal_connect (flash,
|
| 129 | "handle-init",
|
| 130 | G_CALLBACK (on_init),
|
| 131 | NULL); /* user_data */
|
| 132 |
|
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 133 | 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 James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 144 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 145 | /* 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 149 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 150 | /* Export all objects */
|
| 151 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 152 | }
|
| 153 |
|
| 154 | static void
|
| 155 | on_name_acquired (GDBusConnection *connection,
|
| 156 | const gchar *name,
|
| 157 | gpointer user_data)
|
| 158 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 159 | // g_print ("Acquired the name %s\n", name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 160 | }
|
| 161 |
|
| 162 | static void
|
| 163 | on_name_lost (GDBusConnection *connection,
|
| 164 | const gchar *name,
|
| 165 | gpointer user_data)
|
| 166 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 167 | //g_print ("Lost the name %s\n", name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 168 | }
|
| 169 |
|
| 170 | gint
|
| 171 | main (gint argc, gchar *argv[])
|
| 172 | {
|
| 173 | GMainLoop *loop;
|
Norman James | 952b38d | 2015-08-27 21:30:06 -0500 | [diff] [blame] | 174 | cmdline cmd;
|
| 175 | cmd.argc = argc;
|
| 176 | cmd.argv = argv;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 177 |
|
| 178 | guint id;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 179 | loop = g_main_loop_new (NULL, FALSE);
|
| 180 |
|
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 181 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 182 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 183 | 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 James | 952b38d | 2015-08-27 21:30:06 -0500 | [diff] [blame] | 188 | &cmd,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 189 | 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 | }
|