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