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