]> git.ozlabs.org Git - petitboot/blob - lib/pb-protocol/pb-protocol.c
lib/pb-protocol: Fix NULL dereference on non-powerpc
[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 }
211
212 int pb_protocol_boot_status_len(const struct boot_status *status)
213 {
214         return  4 +
215                 4 + optional_strlen(status->message) +
216                 4 + optional_strlen(status->detail) +
217                 4;
218 }
219
220 int pb_protocol_system_info_len(const struct system_info *sysinfo)
221 {
222         unsigned int len, i;
223
224         len =   4 + optional_strlen(sysinfo->type) +
225                 4 + optional_strlen(sysinfo->identifier) +
226                 4 + 4;
227
228         for (i = 0; i < sysinfo->n_interfaces; i++) {
229                 struct interface_info *if_info = sysinfo->interfaces[i];
230                 len +=  4 + if_info->hwaddr_size +
231                         4 + optional_strlen(if_info->name) +
232                         sizeof(if_info->link);
233         }
234
235         for (i = 0; i < sysinfo->n_blockdevs; i++) {
236                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
237                 len +=  4 + optional_strlen(bd_info->name) +
238                         4 + optional_strlen(bd_info->uuid) +
239                         4 + optional_strlen(bd_info->mountpoint);
240         }
241
242         /* BMC MAC */
243         len += HWADDR_SIZE;
244
245         return len;
246 }
247
248 static int pb_protocol_interface_config_len(struct interface_config *conf)
249 {
250         unsigned int len;
251
252         len =   sizeof(conf->hwaddr) +
253                 4 /* conf->ignore */;
254
255         if (conf->ignore)
256                 return len;
257
258         len += 4 /* conf->method */;
259
260         if (conf->method == CONFIG_METHOD_STATIC) {
261                 len += 4 + optional_strlen(conf->static_config.address);
262                 len += 4 + optional_strlen(conf->static_config.gateway);
263                 len += 4 + optional_strlen(conf->static_config.url);
264         }
265
266         return len;
267 }
268
269 int pb_protocol_config_len(const struct config *config)
270 {
271         unsigned int i, len;
272
273         len =   4 /* config->autoboot_enabled */ +
274                 4 /* config->autoboot_timeout_sec */ +
275                 4 /* config->safe_mode */;
276
277         len += 4;
278         for (i = 0; i < config->network.n_interfaces; i++)
279                 len += pb_protocol_interface_config_len(
280                                 config->network.interfaces[i]);
281
282         len += 4;
283         for (i = 0; i < config->network.n_dns_servers; i++)
284                 len += 4 + optional_strlen(config->network.dns_servers[i]);
285
286         len += 4;
287         for (i = 0; i < config->n_autoboot_opts; i++) {
288                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
289                         len += 4 + 4;
290                 else
291                         len += 4 + 4 +
292                                 optional_strlen(config->autoboot_opts[i].uuid);
293         }
294
295         len += 4 + 4; /* ipmi_bootdev, ipmi_bootdev_persistent */
296
297         len += 4; /* allow_writes */
298
299         len += 4 + optional_strlen(config->lang);
300
301         return len;
302 }
303
304 int pb_protocol_url_len(const char *url)
305 {
306         /* url + length field */
307         return 4 + optional_strlen(url);
308 }
309
310 int pb_protocol_serialise_device(const struct device *dev,
311                 char *buf, int buf_len)
312 {
313         char *pos = buf;
314
315         pos += pb_protocol_serialise_string(pos, dev->id);
316         *(enum device_type *)pos = dev->type;
317         pos += sizeof(enum device_type);
318         pos += pb_protocol_serialise_string(pos, dev->name);
319         pos += pb_protocol_serialise_string(pos, dev->description);
320         pos += pb_protocol_serialise_string(pos, dev->icon_file);
321
322         assert(pos <= buf + buf_len);
323         (void)buf_len;
324
325         return 0;
326 }
327
328 int pb_protocol_serialise_boot_option(const struct boot_option *opt,
329                 char *buf, int buf_len)
330 {
331         char *pos = buf;
332
333         pos += pb_protocol_serialise_string(pos, opt->device_id);
334         pos += pb_protocol_serialise_string(pos, opt->id);
335         pos += pb_protocol_serialise_string(pos, opt->name);
336         pos += pb_protocol_serialise_string(pos, opt->description);
337         pos += pb_protocol_serialise_string(pos, opt->icon_file);
338         pos += pb_protocol_serialise_string(pos, opt->boot_image_file);
339         pos += pb_protocol_serialise_string(pos, opt->initrd_file);
340         pos += pb_protocol_serialise_string(pos, opt->dtb_file);
341         pos += pb_protocol_serialise_string(pos, opt->boot_args);
342
343         *(bool *)pos = opt->is_default;
344         pos += sizeof(bool);
345
346         assert(pos <= buf + buf_len);
347         (void)buf_len;
348
349         return 0;
350 }
351
352 int pb_protocol_serialise_boot_command(const struct boot_command *boot,
353                 char *buf, int buf_len)
354 {
355         char *pos = buf;
356
357         pos += pb_protocol_serialise_string(pos, boot->option_id);
358         pos += pb_protocol_serialise_string(pos, boot->boot_image_file);
359         pos += pb_protocol_serialise_string(pos, boot->initrd_file);
360         pos += pb_protocol_serialise_string(pos, boot->dtb_file);
361         pos += pb_protocol_serialise_string(pos, boot->boot_args);
362
363         assert(pos <= buf + buf_len);
364         (void)buf_len;
365
366         return 0;
367 }
368
369 int pb_protocol_serialise_boot_status(const struct boot_status *status,
370                 char *buf, int buf_len)
371 {
372         char *pos = buf;
373
374         *(uint32_t *)pos = __cpu_to_be32(status->type);
375         pos += sizeof(uint32_t);
376
377         pos += pb_protocol_serialise_string(pos, status->message);
378         pos += pb_protocol_serialise_string(pos, status->detail);
379
380         *(uint32_t *)pos = __cpu_to_be32(status->type);
381         pos += sizeof(uint32_t);
382
383         assert(pos <= buf + buf_len);
384         (void)buf_len;
385
386         return 0;
387 }
388
389 int pb_protocol_serialise_system_info(const struct system_info *sysinfo,
390                 char *buf, int buf_len)
391 {
392         char *pos = buf;
393         unsigned int i;
394
395         pos += pb_protocol_serialise_string(pos, sysinfo->type);
396         pos += pb_protocol_serialise_string(pos, sysinfo->identifier);
397
398         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_interfaces);
399         pos += sizeof(uint32_t);
400
401         for (i = 0; i < sysinfo->n_interfaces; i++) {
402                 struct interface_info *if_info = sysinfo->interfaces[i];
403
404                 *(uint32_t *)pos = __cpu_to_be32(if_info->hwaddr_size);
405                 pos += sizeof(uint32_t);
406
407                 memcpy(pos, if_info->hwaddr, if_info->hwaddr_size);
408                 pos += if_info->hwaddr_size;
409
410                 pos += pb_protocol_serialise_string(pos, if_info->name);
411
412                 *(bool *)pos = if_info->link;
413                 pos += sizeof(bool);
414         }
415
416         *(uint32_t *)pos = __cpu_to_be32(sysinfo->n_blockdevs);
417         pos += sizeof(uint32_t);
418
419         for (i = 0; i < sysinfo->n_blockdevs; i++) {
420                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
421
422                 pos += pb_protocol_serialise_string(pos, bd_info->name);
423                 pos += pb_protocol_serialise_string(pos, bd_info->uuid);
424                 pos += pb_protocol_serialise_string(pos, bd_info->mountpoint);
425         }
426
427         if (sysinfo->bmc_mac)
428                 memcpy(pos, sysinfo->bmc_mac, HWADDR_SIZE);
429         else
430                 memset(pos, 0, HWADDR_SIZE);
431         pos += HWADDR_SIZE;
432
433         assert(pos <= buf + buf_len);
434         (void)buf_len;
435
436         return 0;
437 }
438
439 static int pb_protocol_serialise_config_interface(char *buf,
440                 struct interface_config *conf)
441 {
442         char *pos = buf;
443
444         memcpy(pos, conf->hwaddr, sizeof(conf->hwaddr));
445         pos += sizeof(conf->hwaddr);
446
447         *(uint32_t *)pos = conf->ignore;
448         pos += 4;
449
450         if (conf->ignore)
451                 return pos - buf;
452
453         *(uint32_t *)pos = __cpu_to_be32(conf->method);
454         pos += 4;
455
456         if (conf->method == CONFIG_METHOD_STATIC) {
457                 pos += pb_protocol_serialise_string(pos,
458                                 conf->static_config.address);
459                 pos += pb_protocol_serialise_string(pos,
460                                 conf->static_config.gateway);
461                 pos += pb_protocol_serialise_string(pos,
462                                 conf->static_config.url);
463         }
464
465         return pos - buf;
466 }
467
468 int pb_protocol_serialise_config(const struct config *config,
469                 char *buf, int buf_len)
470 {
471         char *pos = buf;
472         unsigned int i;
473
474         *(uint32_t *)pos = config->autoboot_enabled;
475         pos += 4;
476
477         *(uint32_t *)pos = __cpu_to_be32(config->autoboot_timeout_sec);
478         pos += 4;
479
480         *(uint32_t *)pos = config->safe_mode;
481         pos += 4;
482
483         *(uint32_t *)pos = __cpu_to_be32(config->network.n_interfaces);
484         pos += 4;
485         for (i = 0; i < config->network.n_interfaces; i++) {
486                 struct interface_config *iface =
487                         config->network.interfaces[i];
488                 pos += pb_protocol_serialise_config_interface(pos, iface);
489         }
490
491         *(uint32_t *)pos = __cpu_to_be32(config->network.n_dns_servers);
492         pos += 4;
493         for (i = 0; i < config->network.n_dns_servers; i++) {
494                 pos += pb_protocol_serialise_string(pos,
495                                 config->network.dns_servers[i]);
496         }
497
498         *(uint32_t *)pos = __cpu_to_be32(config->n_autoboot_opts);
499         pos += 4;
500         for (i = 0; i < config->n_autoboot_opts; i++) {
501                 *(uint32_t *)pos =
502                         __cpu_to_be32(config->autoboot_opts[i].boot_type);
503                 pos += 4;
504                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
505                         *(uint32_t *)pos =
506                                 __cpu_to_be32(config->autoboot_opts[i].type);
507                         pos += 4;
508                 } else {
509                         pos += pb_protocol_serialise_string(pos,
510                                                 config->autoboot_opts[i].uuid);
511                 }
512         }
513
514         *(uint32_t *)pos = __cpu_to_be32(config->ipmi_bootdev);
515         pos += 4;
516         *(uint32_t *)pos = config->ipmi_bootdev_persistent;
517         pos += 4;
518
519         *(uint32_t *)pos = config->allow_writes;
520         pos += 4;
521
522         pos += pb_protocol_serialise_string(pos, config->lang);
523
524         assert(pos <= buf + buf_len);
525         (void)buf_len;
526
527         return 0;
528 }
529
530 int pb_protocol_serialise_url(const char *url, char *buf, int buf_len)
531 {
532         char *pos = buf;
533
534         pos += pb_protocol_serialise_string(pos, url);
535
536         assert(pos <=buf+buf_len);
537         (void)buf_len;
538
539         return 0;
540 }
541
542 int pb_protocol_write_message(int fd, struct pb_protocol_message *message)
543 {
544         int total_len, rc;
545         char *pos;
546
547         total_len = sizeof(*message) + message->payload_len;
548
549         message->payload_len = __cpu_to_be32(message->payload_len);
550         message->action = __cpu_to_be32(message->action);
551
552         for (pos = (void *)message; total_len;) {
553                 rc = write(fd, pos, total_len);
554
555                 if (rc <= 0)
556                         break;
557
558                 total_len -= rc;
559                 pos += rc;
560         }
561
562         talloc_free(message);
563
564         if (!total_len)
565                 return 0;
566
567         pb_log("%s: failed: %s\n", __func__, strerror(errno));
568         return -1;
569 }
570
571 struct pb_protocol_message *pb_protocol_create_message(void *ctx,
572                 enum pb_protocol_action action, int payload_len)
573 {
574         struct pb_protocol_message *message;
575
576         if (payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
577                 pb_log("%s: payload too big %u/%u\n", __func__, payload_len,
578                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
579                 return NULL;
580         }
581
582         message = talloc_size(ctx, sizeof(*message) + payload_len);
583
584         /* we convert these to big-endian in write_message() */
585         message->action = action;
586         message->payload_len = payload_len;
587
588         return message;
589
590 }
591
592 struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd)
593 {
594         struct pb_protocol_message *message, m;
595         int rc;
596         unsigned int len;
597
598         /* use the stack for the initial 8-byte read */
599
600         rc = read(fd, &m, sizeof(m));
601         if (rc != sizeof(m))
602                 return NULL;
603
604         m.payload_len = __be32_to_cpu(m.payload_len);
605         m.action = __be32_to_cpu(m.action);
606
607         if (m.payload_len > PB_PROTOCOL_MAX_PAYLOAD_SIZE) {
608                 pb_log("%s: payload too big %u/%u\n", __func__, m.payload_len,
609                         PB_PROTOCOL_MAX_PAYLOAD_SIZE);
610                 return NULL;
611         }
612
613         message = talloc_size(ctx, sizeof(m) + m.payload_len);
614         memcpy(message, &m, sizeof(m));
615
616         for (len = 0; len < m.payload_len;) {
617                 rc = read(fd, message->payload + len, m.payload_len - len);
618
619                 if (rc <= 0) {
620                         talloc_free(message);
621                         pb_log("%s: failed (%u): %s\n", __func__, len,
622                                 strerror(errno));
623                         return NULL;
624                 }
625
626                 len += rc;
627         }
628
629         return message;
630 }
631
632
633 int pb_protocol_deserialise_device(struct device *dev,
634                 const struct pb_protocol_message *message)
635 {
636         unsigned int len;
637         const char *pos;
638         int rc = -1;
639
640         len = message->payload_len;
641         pos = message->payload;
642
643         if (read_string(dev, &pos, &len, &dev->id))
644                 goto out;
645
646         if (len < sizeof(enum device_type))
647                 goto out;
648         dev->type = *(enum device_type *)(pos);
649         pos += sizeof(enum device_type);
650         len -= sizeof(enum device_type);
651
652         if (read_string(dev, &pos, &len, &dev->name))
653                 goto out;
654
655         if (read_string(dev, &pos, &len, &dev->description))
656                 goto out;
657
658         if (read_string(dev, &pos, &len, &dev->icon_file))
659                 goto out;
660
661         rc = 0;
662
663 out:
664         return rc;
665 }
666
667 int pb_protocol_deserialise_boot_option(struct boot_option *opt,
668                 const struct pb_protocol_message *message)
669 {
670         unsigned int len;
671         const char *pos;
672         int rc = -1;
673
674         len = message->payload_len;
675         pos = message->payload;
676
677         if (read_string(opt, &pos, &len, &opt->device_id))
678                 goto out;
679
680         if (read_string(opt, &pos, &len, &opt->id))
681                 goto out;
682
683         if (read_string(opt, &pos, &len, &opt->name))
684                 goto out;
685
686         if (read_string(opt, &pos, &len, &opt->description))
687                 goto out;
688
689         if (read_string(opt, &pos, &len, &opt->icon_file))
690                 goto out;
691
692         if (read_string(opt, &pos, &len, &opt->boot_image_file))
693                 goto out;
694
695         if (read_string(opt, &pos, &len, &opt->initrd_file))
696                 goto out;
697
698         if (read_string(opt, &pos, &len, &opt->dtb_file))
699                 goto out;
700
701         if (read_string(opt, &pos, &len, &opt->boot_args))
702                 goto out;
703
704         if (len < sizeof(bool))
705                 goto out;
706         opt->is_default = *(bool *)(pos);
707
708         rc = 0;
709
710 out:
711         return rc;
712 }
713
714 int pb_protocol_deserialise_boot_command(struct boot_command *cmd,
715                 const struct pb_protocol_message *message)
716 {
717         unsigned int len;
718         const char *pos;
719         int rc = -1;
720
721         len = message->payload_len;
722         pos = message->payload;
723
724         if (read_string(cmd, &pos, &len, &cmd->option_id))
725                 goto out;
726
727         if (read_string(cmd, &pos, &len, &cmd->boot_image_file))
728                 goto out;
729
730         if (read_string(cmd, &pos, &len, &cmd->initrd_file))
731                 goto out;
732
733         if (read_string(cmd, &pos, &len, &cmd->dtb_file))
734                 goto out;
735
736         if (read_string(cmd, &pos, &len, &cmd->boot_args))
737                 goto out;
738
739         rc = 0;
740
741 out:
742         return rc;
743 }
744
745 int pb_protocol_deserialise_boot_status(struct boot_status *status,
746                 const struct pb_protocol_message *message)
747 {
748         unsigned int len;
749         const char *pos;
750         int rc = -1;
751
752         len = message->payload_len;
753         pos = message->payload;
754
755         /* first up, the type enum... */
756         if (len < sizeof(uint32_t))
757                 goto out;
758
759         status->type = __be32_to_cpu(*(uint32_t *)(pos));
760
761         switch (status->type) {
762         case BOOT_STATUS_ERROR:
763         case BOOT_STATUS_INFO:
764                 break;
765         default:
766                 goto out;
767         }
768
769         pos += sizeof(uint32_t);
770         len -= sizeof(uint32_t);
771
772         /* message and detail strings */
773         if (read_string(status, &pos, &len, &status->message))
774                 goto out;
775
776         if (read_string(status, &pos, &len, &status->detail))
777                 goto out;
778
779         /* and finally, progress */
780         if (len < sizeof(uint32_t))
781                 goto out;
782
783         status->progress = __be32_to_cpu(*(uint32_t *)(pos));
784
785         /* clamp to 100% */
786         if (status->progress > 100)
787                 status->progress = 100;
788
789         rc = 0;
790
791 out:
792         return rc;
793 }
794
795 int pb_protocol_deserialise_system_info(struct system_info *sysinfo,
796                 const struct pb_protocol_message *message)
797 {
798         unsigned int len, i;
799         const char *pos;
800         int rc = -1;
801
802         len = message->payload_len;
803         pos = message->payload;
804
805         /* type and identifier strings */
806         if (read_string(sysinfo, &pos, &len, &sysinfo->type))
807                 goto out;
808
809         if (read_string(sysinfo, &pos, &len, &sysinfo->identifier))
810                 goto out;
811
812         /* number of interfaces */
813         if (read_u32(&pos, &len, &sysinfo->n_interfaces))
814                 goto out;
815
816         sysinfo->interfaces = talloc_array(sysinfo, struct interface_info *,
817                         sysinfo->n_interfaces);
818
819         for (i = 0; i < sysinfo->n_interfaces; i++) {
820                 struct interface_info *if_info = talloc(sysinfo,
821                                                         struct interface_info);
822
823                 if (read_u32(&pos, &len, &if_info->hwaddr_size))
824                         goto out;
825
826                 if (len < if_info->hwaddr_size)
827                         goto out;
828
829                 if_info->hwaddr = talloc_memdup(if_info, pos,
830                                                 if_info->hwaddr_size);
831                 pos += if_info->hwaddr_size;
832                 len -= if_info->hwaddr_size;
833
834                 if (read_string(if_info, &pos, &len, &if_info->name))
835                         goto out;
836
837                 if_info->link = *(bool *)pos;
838                 pos += sizeof(if_info->link);
839
840                 sysinfo->interfaces[i] = if_info;
841         }
842
843         /* number of interfaces */
844         if (read_u32(&pos, &len, &sysinfo->n_blockdevs))
845                 goto out;
846
847         sysinfo->blockdevs = talloc_array(sysinfo, struct blockdev_info *,
848                         sysinfo->n_blockdevs);
849
850         for (i = 0; i < sysinfo->n_blockdevs; i++) {
851                 struct blockdev_info *bd_info = talloc(sysinfo,
852                                                         struct blockdev_info);
853
854                 if (read_string(bd_info, &pos, &len, &bd_info->name))
855                         goto out;
856
857                 if (read_string(bd_info, &pos, &len, &bd_info->uuid))
858                         goto out;
859
860                 if (read_string(bd_info, &pos, &len, &bd_info->mountpoint))
861                         goto out;
862
863                 sysinfo->blockdevs[i] = bd_info;
864         }
865
866         for (i = 0; i < HWADDR_SIZE; i++) {
867                 if (pos[i] != 0) {
868                         sysinfo->bmc_mac = talloc_memdup(sysinfo, pos, HWADDR_SIZE);
869                         break;
870                 }
871         }
872
873         pos += HWADDR_SIZE;
874         len -= HWADDR_SIZE;
875
876         rc = 0;
877 out:
878         return rc;
879 }
880
881 static int pb_protocol_deserialise_config_interface(const char **buf,
882                 unsigned int *len, struct interface_config *iface)
883 {
884         unsigned int tmp;
885
886         if (*len < sizeof(iface->hwaddr))
887                 return -1;
888
889         memcpy(iface->hwaddr, *buf, sizeof(iface->hwaddr));
890         *buf += sizeof(iface->hwaddr);
891         *len -= sizeof(iface->hwaddr);
892
893         if (read_u32(buf, len, &tmp))
894                 return -1;
895         iface->ignore = !!tmp;
896
897         if (iface->ignore)
898                 return 0;
899
900         if (read_u32(buf, len, &iface->method))
901                 return -1;
902
903         if (iface->method == CONFIG_METHOD_STATIC) {
904                 if (read_string(iface, buf, len, &iface->static_config.address))
905                         return -1;
906
907                 if (read_string(iface, buf, len, &iface->static_config.gateway))
908                         return -1;
909
910                 if (read_string(iface, buf, len, &iface->static_config.url))
911                         return -1;
912         }
913
914         return 0;
915 }
916
917 int pb_protocol_deserialise_config(struct config *config,
918                 const struct pb_protocol_message *message)
919 {
920         unsigned int len, i, tmp;
921         const char *pos;
922         int rc = -1;
923         char *str;
924
925         len = message->payload_len;
926         pos = message->payload;
927
928         if (read_u32(&pos, &len, &tmp))
929                 goto out;
930         config->autoboot_enabled = !!tmp;
931
932         if (read_u32(&pos, &len, &config->autoboot_timeout_sec))
933                 goto out;
934
935         if (read_u32(&pos, &len, &tmp))
936                 goto out;
937         config->safe_mode = !!tmp;
938
939         if (read_u32(&pos, &len, &config->network.n_interfaces))
940                 goto out;
941
942         config->network.interfaces = talloc_array(config,
943                         struct interface_config *, config->network.n_interfaces);
944
945         for (i = 0; i < config->network.n_interfaces; i++) {
946                 struct interface_config *iface = talloc_zero(
947                                 config->network.interfaces,
948                                 struct interface_config);
949                 if (pb_protocol_deserialise_config_interface(&pos, &len, iface))
950                         goto out;
951                 config->network.interfaces[i] = iface;
952         }
953
954         if (read_u32(&pos, &len, &config->network.n_dns_servers))
955                 goto out;
956         config->network.dns_servers = talloc_array(config, const char *,
957                         config->network.n_dns_servers);
958
959         for (i = 0; i < config->network.n_dns_servers; i++) {
960                 if (read_string(config->network.dns_servers, &pos, &len, &str))
961                         goto out;
962                 config->network.dns_servers[i] = str;
963         }
964
965         if (read_u32(&pos, &len, &config->n_autoboot_opts))
966                 goto out;
967         config->autoboot_opts = talloc_array(config, struct autoboot_option,
968                         config->n_autoboot_opts);
969
970         for (i = 0; i < config->n_autoboot_opts; i++) {
971                 if (read_u32(&pos, &len, &tmp))
972                         goto out;
973                 config->autoboot_opts[i].boot_type = (int)tmp;
974                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE) {
975                         if (read_u32(&pos, &len, &tmp))
976                                 goto out;
977                         config->autoboot_opts[i].type = tmp;
978                 } else {
979                         if (read_string(config, &pos, &len, &str))
980                                 goto out;
981                         config->autoboot_opts[i].uuid = str;
982                 }
983         }
984
985         if (read_u32(&pos, &len, &config->ipmi_bootdev))
986                 goto out;
987         if (read_u32(&pos, &len, &tmp))
988                 goto out;
989         config->ipmi_bootdev_persistent = !!tmp;
990
991         if (read_u32(&pos, &len, &tmp))
992                 goto out;
993         config->allow_writes = !!tmp;
994
995         if (read_string(config, &pos, &len, &str))
996                 goto out;
997
998         config->lang = str;
999
1000         rc = 0;
1001
1002 out:
1003         return rc;
1004 }