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