]> git.ozlabs.org Git - petitboot/blobdiff - lib/pb-protocol/pb-protocol.c
pb-protocol: Don't allocate in deserialise functions
[petitboot] / lib / pb-protocol / pb-protocol.c
index fdf24471377160e9a3c51abb87c4197e4e034e1c..c6d8f635ad6a495d5f9bd15b7c6b94e851d375b1 100644 (file)
  * action = 0x2: device remove message
  *  payload:
  *   4-byte len, id
+ *
+ * action = 0x3: boot
+ *  payload:
+ *   4-byte len, boot option id
+ *   4-byte len, boot_image_file
+ *   4-byte len, initrd_file
+ *   4-byte len, boot_args
+ *
  */
 
 void pb_protocol_dump_device(const struct device *dev, const char *text,
@@ -175,6 +183,14 @@ int pb_protocol_device_len(const struct device *dev)
        return len;
 }
 
+int pb_protocol_boot_len(const struct boot_command *boot)
+{
+       return  4 + optional_strlen(boot->option_id) +
+               4 + optional_strlen(boot->boot_image_file) +
+               4 + optional_strlen(boot->initrd_file) +
+               4 + optional_strlen(boot->boot_args);
+}
+
 int pb_protocol_serialise_device(const struct device *dev, char *buf, int buf_len)
 {
        struct boot_option *opt;
@@ -215,6 +231,22 @@ int pb_protocol_serialise_device(const struct device *dev, char *buf, int buf_le
        return 0;
 }
 
+int pb_protocol_serialise_boot_command(const struct boot_command *boot,
+               char *buf, int buf_len)
+{
+       char *pos = buf;
+
+       pos += pb_protocol_serialise_string(pos, boot->option_id);
+       pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
+       pos += pb_protocol_serialise_string(pos, boot->initrd_file);
+       pos += pb_protocol_serialise_string(pos, boot->boot_args);
+
+       assert(pos <= buf + buf_len);
+       (void)buf_len;
+
+       return 0;
+}
+
 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
 {
        int total_len, rc;
@@ -306,10 +338,9 @@ struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
 }
 
 
-struct device *pb_protocol_deserialise_device(void *ctx,
+int pb_protocol_deserialise_device(struct device *dev,
                const struct pb_protocol_message *message)
 {
-       struct device *dev;
        const char *pos;
        int i, n_options;
        unsigned int len;
@@ -317,8 +348,6 @@ struct device *pb_protocol_deserialise_device(void *ctx,
        len = message->payload_len;
        pos = message->payload;
 
-       dev = talloc(ctx, struct device);
-
        if (read_string(dev, &pos, &len, &dev->id))
                goto out_err;
 
@@ -366,9 +395,35 @@ struct device *pb_protocol_deserialise_device(void *ctx,
                list_add(&dev->boot_options, &opt->list);
        }
 
-       return dev;
+       return 0;
+
+out_err:
+       return -1;
+}
+
+int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
+               const struct pb_protocol_message *message)
+{
+       const char *pos;
+       unsigned int len;
+
+       len = message->payload_len;
+       pos = message->payload;
+
+       if (read_string(cmd, &pos, &len, &cmd->option_id))
+               goto out_err;
+
+       if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
+               goto out_err;
+
+       if (read_string(cmd, &pos, &len, &cmd->initrd_file))
+               goto out_err;
+
+       if (read_string(cmd, &pos, &len, &cmd->boot_args))
+               goto out_err;
+
+       return 0;
 
 out_err:
-       talloc_free(dev);
-       return NULL;
+       return -1;
 }