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