lib: Add support for tracking boot consoles
[petitboot] / lib / pb-protocol / pb-protocol.c
1
2 #include <assert.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <asm/byteorder.h>
8
9 #include <talloc/talloc.h>
10 #include <list/list.h>
11 #include <log/log.h>
12
13 #include "pb-protocol.h"
14
15
16 /* Message format:
17  *
18  * 4-byte action, determines the remaining message content
19  * 4-byte total payload len
20  *   - not including action and payload len header
21  *
22  * action = 0x1: device add message
23  *  payload:
24  *   4-byte len, id
25  *   1-byte type
26  *   4-byte len, name
27  *   4-byte len, description
28  *   4-byte len, icon_file
29  *
30  *   4-byte option count
31  *   for each option:
32  *    4-byte len, id
33  *    4-byte len, name
34  *    4-byte len, description
35  *    4-byte len, icon_file
36  *    4-byte len, boot_image_file
37  *    4-byte len, initrd_file
38  *    4-byte len, dtb_file
39  *    4-byte len, boot_args
40  *
41  * action = 0x2: device remove message
42  *  payload:
43  *   4-byte len, id
44  *
45  * action = 0x3: boot
46  *  payload:
47  *   4-byte len, boot option id
48  *   4-byte len, boot_image_file
49  *   4-byte len, initrd_file
50  *   4-byte len, dtb_file
51  *   4-byte len, boot_args
52  *
53  */
54
55 void pb_protocol_dump_device(const struct device *dev, const char *text,
56         FILE *stream)
57 {
58         struct boot_option *opt;
59
60         fprintf(stream, "%snew dev:\n", text);
61         fprintf(stream, "%s\tid:   %s\n", text, dev->id);
62         fprintf(stream, "%s\tname: %s\n", text, dev->name);
63         fprintf(stream, "%s\tdesc: %s\n", text, dev->description);
64         fprintf(stream, "%s\ticon: %s\n", text, dev->icon_file);
65         fprintf(stream, "%s\tboot options:\n", text);
66         list_for_each_entry(&dev->boot_options, opt, list) {
67                 fprintf(stream, "%s\t\tid:   %s\n", text, opt->id);
68                 fprintf(stream, "%s\t\tname: %s\n", text, opt->name);
69                 fprintf(stream, "%s\t\tdesc: %s\n", text, opt->description);
70                 fprintf(stream, "%s\t\ticon: %s\n", text, opt->icon_file);
71                 fprintf(stream, "%s\t\tboot: %s\n", text, opt->boot_image_file);
72                 fprintf(stream, "%s\t\tinit: %s\n", text, opt->initrd_file);
73                 fprintf(stream, "%s\t\tdtb:  %s\n", text, opt->dtb_file);
74                 fprintf(stream, "%s\t\targs: %s\n", text, opt->boot_args);
75         }
76 }
77
78 int pb_protocol_device_cmp(const struct device *a, const struct device *b)
79 {
80         return !strcmp(a->id, b->id);
81 }
82
83 int pb_protocol_boot_option_cmp(const struct boot_option *a,
84         const struct boot_option *b)
85 {
86         return !strcmp(a->id, b->id);
87 }
88
89 /* Write a string into the buffer, starting at pos.
90  *
91  * Returns the total length used for the write, including length header.
92  */
93 int pb_protocol_serialise_string(char *pos, const char *str)
94 {
95         int len = 0;
96
97         if (str)
98                 len = strlen(str);
99
100         *(uint32_t *)pos = __cpu_to_be32(len);
101         pos += sizeof(uint32_t);
102
103         memcpy(pos, str, len);
104
105         return len + sizeof(uint32_t);
106 }
107
108 /* Read a string from a buffer, allocating the new string as necessary.
109  *
110  * @param[in] ctx       The talloc context to base the allocation on
111  * @param[in,out] pos   Where to start reading
112  * @param[in,out] len   The amount of data remaining in the buffer
113  * @param[out] str      Pointer to resuling string
114  * @return              zero on success, non-zero on failure
115  */
116 static int read_string(void *ctx, const char **pos, unsigned int *len,
117         char **str)
118 {
119         uint32_t str_len, read_len;
120
121         if (*len < sizeof(uint32_t))
122                 return -1;
123
124         str_len = __be32_to_cpu(*(uint32_t *)(*pos));
125         read_len = sizeof(uint32_t);
126
127         if (read_len + str_len > *len)
128                 return -1;
129
130         if (str_len == 0)
131                 *str = NULL;
132         else
133                 *str = talloc_strndup(ctx, *pos + read_len, str_len);
134
135         read_len += str_len;
136
137         /* all ok, update the caller's pointers */
138         *pos += read_len;
139         *len -= read_len;
140
141         return 0;
142 }
143
144 static int read_u32(const char **pos, unsigned int *len, unsigned int *p)
145 {
146         if (*len < sizeof(uint32_t))
147                 return -1;
148
149         *p = (unsigned int)__be32_to_cpu(*(uint32_t *)(*pos));
150         *pos += sizeof(uint32_t);
151         *len -= sizeof(uint32_t);
152
153         return 0;
154 }
155
156 char *pb_protocol_deserialise_string(void *ctx,
157                 const struct pb_protocol_message *message)
158 {
159         const char *buf;
160         char *str;
161         unsigned int len;
162
163         len = message->payload_len;
164         buf = message->payload;
165
166         if (read_string(ctx, &buf, &len, &str))
167                 return NULL;
168
169         return str;
170 }
171
172 static int optional_strlen(const char *str)
173 {
174         if (!str)
175                 return 0;
176         return strlen(str);
177 }
178
179 int pb_protocol_device_len(const struct device *dev)
180 {
181         return  4 + optional_strlen(dev->id) +
182                 sizeof(dev->type) +
183                 4 + optional_strlen(dev->name) +
184                 4 + optional_strlen(dev->description) +
185                 4 + optional_strlen(dev->icon_file);
186 }
187
188 int pb_protocol_boot_option_len(const struct boot_option *opt)
189 {
190
191         return  4 + optional_strlen(opt->device_id) +
192                 4 + optional_strlen(opt->id) +
193                 4 + optional_strlen(opt->name) +
194                 4 + optional_strlen(opt->description) +
195                 4 + optional_strlen(opt->icon_file) +
196                 4 + optional_strlen(opt->boot_image_file) +
197                 4 + optional_strlen(opt->initrd_file) +
198                 4 + optional_strlen(opt->dtb_file) +
199                 4 + optional_strlen(opt->boot_args) +
200                 sizeof(opt->is_default);
201 }
202
203 int pb_protocol_boot_len(const struct boot_command *boot)
204 {
205         return  4 + optional_strlen(boot->option_id) +
206                 4 + optional_strlen(boot->boot_image_file) +
207                 4 + optional_strlen(boot->initrd_file) +
208                 4 + optional_strlen(boot->dtb_file) +
209                 4 + optional_strlen(boot->boot_args) +
210                 4 + optional_strlen(boot->tty);
211 }
212
213 int pb_protocol_boot_status_len(const struct boot_status *status)
214 {
215         return  4 +
216                 4 + optional_strlen(status->message) +
217                 4 + optional_strlen(status->detail) +
218                 4;
219 }
220
221 int pb_protocol_system_info_len(const struct system_info *sysinfo)
222 {
223         unsigned int len, i;
224
225         len =   4 + optional_strlen(sysinfo->type) +
226                 4 + optional_strlen(sysinfo->identifier) +
227                 4 + 4;
228
229         len +=  4;
230         for (i = 0; i < sysinfo->n_primary; i++)
231                 len += 4 + optional_strlen(sysinfo->platform_primary[i]);
232         len +=  4;
233         for (i = 0; i < sysinfo->n_other; i++)
234                 len += 4 + optional_strlen(sysinfo->platform_other[i]);
235
236         len +=  4;
237         for (i = 0; i < sysinfo->n_bmc_current; i++)
238                 len += 4 + optional_strlen(sysinfo->bmc_current[i]);
239         len +=  4;
240         for (i = 0; i < sysinfo->n_bmc_golden; i++)
241                 len += 4 + optional_strlen(sysinfo->bmc_golden[i]);
242
243         for (i = 0; i < sysinfo->n_interfaces; i++) {
244                 struct interface_info *if_info = sysinfo->interfaces[i];
245                 len +=  4 + if_info->hwaddr_size +
246                         4 + optional_strlen(if_info->name) +
247                         sizeof(if_info->link);
248         }
249
250         for (i = 0; i < sysinfo->n_blockdevs; i++) {
251                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
252                 len +=  4 + optional_strlen(bd_info->name) +
253                         4 + optional_strlen(bd_info->uuid) +
254                         4 + optional_strlen(bd_info->mountpoint);
255         }
256
257         /* BMC MAC */
258         len += HWADDR_SIZE;
259
260         return len;
261 }
262
263 static int pb_protocol_interface_config_len(struct interface_config *conf)
264 {
265         unsigned int len;
266
267         len =   sizeof(conf->hwaddr) +
268                 4 /* conf->ignore */;
269
270         if (conf->ignore)
271                 return len;
272
273         len += 4 /* conf->method */;
274
275         if (conf->method == CONFIG_METHOD_STATIC) {
276                 len += 4 + optional_strlen(conf->static_config.address);
277                 len += 4 + optional_strlen(conf->static_config.gateway);
278                 len += 4 + optional_strlen(conf->static_config.url);
279         }
280
281         return len;
282 }
283
284 int pb_protocol_config_len(const struct config *config)
285 {
286         unsigned int i, len;
287
288         len =   4 /* config->autoboot_enabled */ +
289                 4 /* config->autoboot_timeout_sec */ +
290                 4 /* config->safe_mode */;
291
292         len += 4;
293         for (i = 0; i < config->network.n_interfaces; i++)
294                 len += pb_protocol_interface_config_len(
295                                 config->network.interfaces[i]);
296
297         len += 4;
298         for (i = 0; i < config->network.n_dns_servers; i++)
299                 len += 4 + optional_strlen(config->network.dns_servers[i]);
300
301         len += 4;
302         for (i = 0; i < config->n_autoboot_opts; i++) {
303                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
304                         len += 4 + 4;
305                 else
306                         len += 4 + 4 +
307                                 optional_strlen(config->autoboot_opts[i].uuid);
308         }
309
310         len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
311
312         len += 4; /* allow_writes */
313
314         len += 4; /* n_tty */
315         for (i = 0; i < config->n_tty; i++)
316                 len += 4 + optional_strlen(config->tty_list[i]);
317
318         len += 4 + optional_strlen(config->boot_tty);
319
320         len += 4 + optional_strlen(config->lang);
321
322         return len;
323 }
324
325 int pb_protocol_url_len(const char *url)
326 {
327         /* url + length field */
328         return 4 + optional_strlen(url);
329 }
330
331 int pb_protocol_serialise_device(const struct device *dev,
332                 char *buf, int buf_len)
333 {
334         char *pos = buf;
335
336         pos += pb_protocol_serialise_string(pos, dev->id);
337         *(enum device_type *)pos = dev->type;
338         pos += sizeof(enum device_type);
339         pos += pb_protocol_serialise_string(pos, dev->name);
340         pos += pb_protocol_serialise_string(pos, dev->description);
341         pos += pb_protocol_serialise_string(pos, dev->icon_file);
342
343         assert(pos <= buf + buf_len);
344         (void)buf_len;
345
346         return 0;
347 }
348
349 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
350                 char *buf, int buf_len)
351 {
352         char *pos = buf;
353
354         pos += pb_protocol_serialise_string(pos, opt->device_id);
355         pos += pb_protocol_serialise_string(pos, opt->id);
356         pos += pb_protocol_serialise_string(pos, opt->name);
357         pos += pb_protocol_serialise_string(pos, opt->description);
358         pos += pb_protocol_serialise_string(pos, opt->icon_file);
359         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
360         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
361         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
362         pos += pb_protocol_serialise_string(pos, opt->boot_args);
363
364         *(bool *)pos = opt->is_default;
365         pos += sizeof(bool);
366
367         assert(pos <= buf + buf_len);
368         (void)buf_len;
369
370         return 0;
371 }
372
373 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
374                 char *buf, int buf_len)
375 {
376         char *pos = buf;
377
378         pos += pb_protocol_serialise_string(pos, boot->option_id);
379         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
380         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
381         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
382         pos += pb_protocol_serialise_string(pos, boot->boot_args);
383         pos += pb_protocol_serialise_string(pos, boot->tty);
384
385         assert(pos <= buf + buf_len);
386         (void)buf_len;
387
388         return 0;
389 }
390
391 int pb_protocol_serialise_boot_status(const struct boot_status *status,
392                 char *buf, int buf_len)
393 {
394         char *pos = buf;
395
396         *(uint32_t *)pos = __cpu_to_be32(status->type);
397         pos += sizeof(uint32_t);
398
399         pos += pb_protocol_serialise_string(pos, status->message);
400         pos += pb_protocol_serialise_string(pos, status->detail);
401
402         *(uint32_t *)pos = __cpu_to_be32(status->type);
403         pos += sizeof(uint32_t);
404
405         assert(pos <= buf + buf_len);
406         (void)buf_len;
407
408         return 0;
409 }
410
411 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
412                 char *buf, int buf_len)
413 {
414         char *pos = buf;
415         unsigned int i;
416
417         pos += pb_protocol_serialise_string(pos, sysinfo->type);
418         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
419
420         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_primary);
421         pos += sizeof(uint32_t);
422         for (i = 0; i < sysinfo->n_primary; i++)
423                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_primary[i]);
424
425         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_other);
426         pos += sizeof(uint32_t);
427         for (i = 0; i < sysinfo->n_other; i++)
428                 pos += pb_protocol_serialise_string(pos, sysinfo->platform_other[i]);
429
430         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_current);
431         pos += sizeof(uint32_t);
432         for (i = 0; i < sysinfo->n_bmc_current; i++)
433                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_current[i]);
434
435         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_bmc_golden);
436         pos += sizeof(uint32_t);
437         for (i = 0; i < sysinfo->n_bmc_golden; i++)
438                 pos += pb_protocol_serialise_string(pos, sysinfo->bmc_golden[i]);
439
440         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
441         pos += sizeof(uint32_t);
442
443         for (i = 0; i < sysinfo->n_interfaces; i++) {
444                 struct interface_info *if_info = sysinfo->interfaces[i];
445
446                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
447                 pos += sizeof(uint32_t);
448
449                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
450                 pos += if_info->hwaddr_size;
451
452                 pos += pb_protocol_serialise_string(pos, if_info->name);
453
454                 *(bool *)pos = if_info->link;
455                 pos += sizeof(bool);
456         }
457
458         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
459         pos += sizeof(uint32_t);
460
461         for (i = 0; i < sysinfo->n_blockdevs; i++) {
462                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
463
464                 pos += pb_protocol_serialise_string(pos, bd_info->name);
465                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
466                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
467         }
468
469         if (sysinfo->bmc_mac)
470                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
471         else
472                 memset(pos, 0, HWADDR_SIZE);
473         pos += HWADDR_SIZE;
474
475         assert(pos <= buf + buf_len);
476         (void)buf_len;
477
478         return 0;
479 }
480
481 static int pb_protocol_serialise_config_interface(char *buf,
482                 struct interface_config *conf)
483 {
484         char *pos = buf;
485
486         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
487         pos += sizeof(conf->hwaddr);
488
489         *(uint32_t *)pos = conf->ignore;
490         pos += 4;
491
492         if (conf->ignore)
493                 return pos - buf;
494
495         *(uint32_t *)pos = __cpu_to_be32(conf->method);
496         pos += 4;
497
498         if (conf->method == CONFIG_METHOD_STATIC) {
499                 pos += pb_protocol_serialise_string(pos,
500                                 conf->static_config.address);
501                 pos += pb_protocol_serialise_string(pos,
502                                 conf->static_config.gateway);
503                 pos += pb_protocol_serialise_string(pos,
504                                 conf->static_config.url);
505         }
506
507         return pos - buf;
508 }
509
510 int pb_protocol_serialise_config(const struct config *config,
511                 char *buf, int buf_len)
512 {
513         char *pos = buf;
514         unsigned int i;
515
516         *(uint32_t *)pos = config->autoboot_enabled;
517         pos += 4;
518
519         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
520         pos += 4;
521
522         *(uint32_t *)pos = config->safe_mode;
523         pos += 4;
524
525         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
526         pos += 4;
527         for (i = 0; i < config->network.n_interfaces; i++) {
528                 struct interface_config *iface =
529                         config->network.interfaces[i];
530                 pos += pb_protocol_serialise_config_interface(pos, iface);
531         }
532
533         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
534         pos += 4;
535         for (i = 0; i < config->network.n_dns_servers; i++) {
536                 pos += pb_protocol_serialise_string(pos,
537                                 config->network.dns_servers[i]);
538         }
539
540         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
541         pos += 4;
542         for (i = 0; i < config->n_autoboot_opts; i++) {
543                 *(uint32_t *)pos =
544                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
545                 pos += 4;
546                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
547                         *(uint32_t *)pos =
548                                 __cpu_to_be32(config->autoboot_opts[i].type);
549                         pos += 4;
550                 } else {
551                         pos += pb_protocol_serialise_string(pos,
552                                                 config->autoboot_opts[i].uuid);
553                 }
554         }
555
556         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
557         pos += 4;
558         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
559         pos += 4;
560
561         *(uint32_t *)pos = config->allow_writes;
562         pos += 4;
563
564         *(uint32_t *)pos = __cpu_to_be32(config->n_tty);
565         pos += 4;
566         for (i = 0; i < config->n_tty; i++)
567                 pos += pb_protocol_serialise_string(pos, config->tty_list[i]);
568
569         pos += pb_protocol_serialise_string(pos, config->boot_tty);
570
571         pos += pb_protocol_serialise_string(pos, config->lang);
572
573         assert(pos <= buf + buf_len);
574         (void)buf_len;
575
576         return 0;
577 }
578
579 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
580 {
581         char *pos = buf;
582
583         pos += pb_protocol_serialise_string(pos, url);
584
585         assert(pos <=buf+buf_len);
586         (void)buf_len;
587
588         return 0;
589 }
590
591 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
592 {
593         int total_len, rc;
594         char *pos;
595
596         total_len = sizeof(*message) + message->payload_len;
597
598         message->payload_len = __cpu_to_be32(message->payload_len);
599         message->action = __cpu_to_be32(message->action);
600
601         for (pos = (void *)message; total_len;) {
602                 rc = write(fd, pos, total_len);
603
604                 if (rc <= 0)
605                         break;
606
607                 total_len -= rc;
608                 pos += rc;
609         }
610
611         talloc_free(message);
612
613         if (!total_len)
614                 return 0;
615
616         pb_log("%s: failed: %s\n", __func__, strerror(errno));
617         return -1;
618 }
619
620 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
621                 enum pb_protocol_action action, int payload_len)
622 {
623         struct pb_protocol_message *message;
624
625         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
626                 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
627                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
628                 return NULL;
629         }
630
631         message = talloc_size(ctx, sizeof(*message) + payload_len);
632
633         /* we convert these to big-endian in write_message() */
634         message->action = action;
635         message->payload_len = payload_len;
636
637         return message;
638
639 }
640
641 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
642 {
643         struct pb_protocol_message *message, m;
644         int rc;
645         unsigned int len;
646
647         /* use the stack for the initial 8-byte read */
648
649         rc = read(fd, &m, sizeof(m));
650         if (rc != sizeof(m))
651                 return NULL;
652
653         m.payload_len = __be32_to_cpu(m.payload_len);
654         m.action = __be32_to_cpu(m.action);
655
656         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
657                 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
658                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
659                 return NULL;
660         }
661
662         message = talloc_size(ctx, sizeof(m) + m.payload_len);
663         memcpy(message, &m, sizeof(m));
664
665         for (len = 0; len < m.payload_len;) {
666                 rc = read(fd, message->payload + len, m.payload_len - len);
667
668                 if (rc <= 0) {
669                         talloc_free(message);
670                         pb_log("%s: failed (%u): %s\n", __func__, len,
671                                 strerror(errno));
672                         return NULL;
673                 }
674
675                 len += rc;
676         }
677
678         return message;
679 }
680
681
682 int pb_protocol_deserialise_device(struct device *dev,
683                 const struct pb_protocol_message *message)
684 {
685         unsigned int len;
686         const char *pos;
687         int rc = -1;
688
689         len = message->payload_len;
690         pos = message->payload;
691
692         if (read_string(dev, &pos, &len, &dev->id))
693                 goto out;
694
695         if (len < sizeof(enum device_type))
696                 goto out;
697         dev->type = *(enum device_type *)(pos);
698         pos += sizeof(enum device_type);
699         len -= sizeof(enum device_type);
700
701         if (read_string(dev, &pos, &len, &dev->name))
702                 goto out;
703
704         if (read_string(dev, &pos, &len, &dev->description))
705                 goto out;
706
707         if (read_string(dev, &pos, &len, &dev->icon_file))
708                 goto out;
709
710         rc = 0;
711
712 out:
713         return rc;
714 }
715
716 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
717                 const struct pb_protocol_message *message)
718 {
719         unsigned int len;
720         const char *pos;
721         int rc = -1;
722
723         len = message->payload_len;
724         pos = message->payload;
725
726         if (read_string(opt, &pos, &len, &opt->device_id))
727                 goto out;
728
729         if (read_string(opt, &pos, &len, &opt->id))
730                 goto out;
731
732         if (read_string(opt, &pos, &len, &opt->name))
733                 goto out;
734
735         if (read_string(opt, &pos, &len, &opt->description))
736                 goto out;
737
738         if (read_string(opt, &pos, &len, &opt->icon_file))
739                 goto out;
740
741         if (read_string(opt, &pos, &len, &opt->boot_image_file))
742                 goto out;
743
744         if (read_string(opt, &pos, &len, &opt->initrd_file))
745                 goto out;
746
747         if (read_string(opt, &pos, &len, &opt->dtb_file))
748                 goto out;
749
750         if (read_string(opt, &pos, &len, &opt->boot_args))
751                 goto out;
752
753         if (len < sizeof(bool))
754                 goto out;
755         opt->is_default = *(bool *)(pos);
756
757         rc = 0;
758
759 out:
760         return rc;
761 }
762
763 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
764                 const struct pb_protocol_message *message)
765 {
766         unsigned int len;
767         const char *pos;
768         int rc = -1;
769
770         len = message->payload_len;
771         pos = message->payload;
772
773         if (read_string(cmd, &pos, &len, &cmd->option_id))
774                 goto out;
775
776         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
777                 goto out;
778
779         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
780                 goto out;
781
782         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
783                 goto out;
784
785         if (read_string(cmd, &pos, &len, &cmd->boot_args))
786                 goto out;
787
788         if (read_string(cmd, &pos, &len, &cmd->tty))
789                 goto out;
790
791         rc = 0;
792
793 out:
794         return rc;
795 }
796
797 int pb_protocol_deserialise_boot_status(struct boot_status *status,
798                 const struct pb_protocol_message *message)
799 {
800         unsigned int len;
801         const char *pos;
802         int rc = -1;
803
804         len = message->payload_len;
805         pos = message->payload;
806
807         /* first up, the type enum... */
808         if (len < sizeof(uint32_t))
809                 goto out;
810
811         status->type = __be32_to_cpu(*(uint32_t *)(pos));
812
813         switch (status->type) {
814         case BOOT_STATUS_ERROR:
815         case BOOT_STATUS_INFO:
816                 break;
817         default:
818                 goto out;
819         }
820
821         pos += sizeof(uint32_t);
822         len -= sizeof(uint32_t);
823
824         /* message and detail strings */
825         if (read_string(status, &pos, &len, &status->message))
826                 goto out;
827
828         if (read_string(status, &pos, &len, &status->detail))
829                 goto out;
830
831         /* and finally, progress */
832         if (len < sizeof(uint32_t))
833                 goto out;
834
835         status->progress = __be32_to_cpu(*(uint32_t *)(pos));
836
837         /* clamp to 100% */
838         if (status->progress > 100)
839                 status->progress = 100;
840
841         rc = 0;
842
843 out:
844         return rc;
845 }
846
847 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
848                 const struct pb_protocol_message *message)
849 {
850         unsigned int len, i;
851         const char *pos;
852         int rc = -1;
853         char *tmp;
854
855         len = message->payload_len;
856         pos = message->payload;
857
858         /* type and identifier strings */
859         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
860                 goto out;
861
862         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
863                 goto out;
864
865         /* Platform version strings for openpower platforms */
866         if (read_u32(&pos, &len, &sysinfo->n_primary))
867                 goto out;
868         sysinfo->platform_primary = talloc_array(sysinfo, char *,
869                                                 sysinfo->n_primary);
870         for (i = 0; i < sysinfo->n_primary; i++) {
871                 if (read_string(sysinfo, &pos, &len, &tmp))
872                         goto out;
873                 sysinfo->platform_primary[i] = talloc_strdup(sysinfo, tmp);
874         }
875
876         if (read_u32(&pos, &len, &sysinfo->n_other))
877                 goto out;
878         sysinfo->platform_other = talloc_array(sysinfo, char *,
879                                                 sysinfo->n_other);
880         for (i = 0; i < sysinfo->n_other; i++) {
881                 if (read_string(sysinfo, &pos, &len, &tmp))
882                         goto out;
883                 sysinfo->platform_other[i] = talloc_strdup(sysinfo, tmp);
884         }
885
886         /* BMC version strings for openpower platforms */
887         if (read_u32(&pos, &len, &sysinfo->n_bmc_current))
888                 goto out;
889         sysinfo->bmc_current = talloc_array(sysinfo, char *,
890                                                 sysinfo->n_bmc_current);
891         for (i = 0; i < sysinfo->n_bmc_current; i++) {
892                 if (read_string(sysinfo, &pos, &len, &tmp))
893                         goto out;
894                 sysinfo->bmc_current[i] = talloc_strdup(sysinfo, tmp);
895         }
896
897         if (read_u32(&pos, &len, &sysinfo->n_bmc_golden))
898                 goto out;
899         sysinfo->bmc_golden = talloc_array(sysinfo, char *,
900                                                 sysinfo->n_bmc_golden);
901         for (i = 0; i < sysinfo->n_bmc_golden; i++) {
902                 if (read_string(sysinfo, &pos, &len, &tmp))
903                         goto out;
904                 sysinfo->bmc_golden[i] = talloc_strdup(sysinfo, tmp);
905         }
906
907         /* number of interfaces */
908         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
909                 goto out;
910
911         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
912                         sysinfo->n_interfaces);
913
914         for (i = 0; i < sysinfo->n_interfaces; i++) {
915                 struct interface_info *if_info = talloc(sysinfo,
916                                                         struct interface_info);
917
918                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
919                         goto out;
920
921                 if (len < if_info->hwaddr_size)
922                         goto out;
923
924                 if_info->hwaddr = talloc_memdup(if_info, pos,
925                                                 if_info->hwaddr_size);
926                 pos += if_info->hwaddr_size;
927                 len -= if_info->hwaddr_size;
928
929                 if (read_string(if_info, &pos, &len, &if_info->name))
930                         goto out;
931
932                 if_info->link = *(bool *)pos;
933                 pos += sizeof(if_info->link);
934
935                 sysinfo->interfaces[i] = if_info;
936         }
937
938         /* number of interfaces */
939         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
940                 goto out;
941
942         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
943                         sysinfo->n_blockdevs);
944
945         for (i = 0; i < sysinfo->n_blockdevs; i++) {
946                 struct blockdev_info *bd_info = talloc(sysinfo,
947                                                         struct blockdev_info);
948
949                 if (read_string(bd_info, &pos, &len, &bd_info->name))
950                         goto out;
951
952                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
953                         goto out;
954
955                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
956                         goto out;
957
958                 sysinfo->blockdevs[i] = bd_info;
959         }
960
961         for (i = 0; i < HWADDR_SIZE; i++) {
962                 if (pos[i] != 0) {
963                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
964                         break;
965                 }
966         }
967
968         pos += HWADDR_SIZE;
969         len -= HWADDR_SIZE;
970
971         rc = 0;
972 out:
973         return rc;
974 }
975
976 static int pb_protocol_deserialise_config_interface(const char **buf,
977                 unsigned int *len, struct interface_config *iface)
978 {
979         unsigned int tmp;
980
981         if (*len < sizeof(iface->hwaddr))
982                 return -1;
983
984         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
985         *buf += sizeof(iface->hwaddr);
986         *len -= sizeof(iface->hwaddr);
987
988         if (read_u32(buf, len, &tmp))
989                 return -1;
990         iface->ignore = !!tmp;
991
992         if (iface->ignore)
993                 return 0;
994
995         if (read_u32(buf, len, &iface->method))
996                 return -1;
997
998         if (iface->method == CONFIG_METHOD_STATIC) {
999                 if (read_string(iface, buf, len, &iface->static_config.address))
1000                         return -1;
1001
1002                 if (read_string(iface, buf, len, &iface->static_config.gateway))
1003                         return -1;
1004
1005                 if (read_string(iface, buf, len, &iface->static_config.url))
1006                         return -1;
1007         }
1008
1009         return 0;
1010 }
1011
1012 int pb_protocol_deserialise_config(struct config *config,
1013                 const struct pb_protocol_message *message)
1014 {
1015         unsigned int len, i, tmp;
1016         const char *pos;
1017         int rc = -1;
1018         char *str;
1019
1020         len = message->payload_len;
1021         pos = message->payload;
1022
1023         if (read_u32(&pos, &len, &tmp))
1024                 goto out;
1025         config->autoboot_enabled = !!tmp;
1026
1027         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
1028                 goto out;
1029
1030         if (read_u32(&pos, &len, &tmp))
1031                 goto out;
1032         config->safe_mode = !!tmp;
1033
1034         if (read_u32(&pos, &len, &config->network.n_interfaces))
1035                 goto out;
1036
1037         config->network.interfaces = talloc_array(config,
1038                         struct interface_config *, config->network.n_interfaces);
1039
1040         for (i = 0; i < config->network.n_interfaces; i++) {
1041                 struct interface_config *iface = talloc_zero(
1042                                 config->network.interfaces,
1043                                 struct interface_config);
1044                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
1045                         goto out;
1046                 config->network.interfaces[i] = iface;
1047         }
1048
1049         if (read_u32(&pos, &len, &config->network.n_dns_servers))
1050                 goto out;
1051         config->network.dns_servers = talloc_array(config, const char *,
1052                         config->network.n_dns_servers);
1053
1054         for (i = 0; i < config->network.n_dns_servers; i++) {
1055                 if (read_string(config->network.dns_servers, &pos, &len, &str))
1056                         goto out;
1057                 config->network.dns_servers[i] = str;
1058         }
1059
1060         if (read_u32(&pos, &len, &config->n_autoboot_opts))
1061                 goto out;
1062         config->autoboot_opts = talloc_array(config, struct autoboot_option,
1063                         config->n_autoboot_opts);
1064
1065         for (i = 0; i < config->n_autoboot_opts; i++) {
1066                 if (read_u32(&pos, &len, &tmp))
1067                         goto out;
1068                 config->autoboot_opts[i].boot_type = (int)tmp;
1069                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
1070                         if (read_u32(&pos, &len, &tmp))
1071                                 goto out;
1072                         config->autoboot_opts[i].type = tmp;
1073                 } else {
1074                         if (read_string(config, &pos, &len, &str))
1075                                 goto out;
1076                         config->autoboot_opts[i].uuid = str;
1077                 }
1078         }
1079
1080         if (read_u32(&pos, &len, &config->ipmi_bootdev))
1081                 goto out;
1082         if (read_u32(&pos, &len, &tmp))
1083                 goto out;
1084         config->ipmi_bootdev_persistent = !!tmp;
1085
1086         if (read_u32(&pos, &len, &tmp))
1087                 goto out;
1088         config->allow_writes = !!tmp;
1089
1090         if (read_u32(&pos, &len, &config->n_tty))
1091                 goto out;
1092
1093         config->tty_list = talloc_array(config, char *, config->n_tty);
1094         for (i = 0; i < config->n_tty; i++) {
1095                 if (read_string(config->tty_list, &pos, &len, &str))
1096                         goto out;
1097                 config->tty_list[i] = str;
1098         }
1099
1100         if (read_string(config, &pos, &len, &str))
1101                 goto out;
1102
1103         config->boot_tty = str;
1104
1105         if (read_string(config, &pos, &len, &str))
1106                 goto out;
1107
1108         config->lang = str;
1109
1110         rc = 0;
1111
1112 out:
1113         return rc;
1114 }