shuffler: Actually get the shuffler size sanely
[syslinux/sherbszt.git] / core / sysappend.c
blob890fb63bd88e3c83c4d34ec5760d74bc371e1ed7
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2011 Intel Corporation; author: H. Peter Anvin
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdbool.h>
16 #include "core.h"
19 * sysappend.c
23 __export uint32_t SysAppends; /* Configuration variable */
24 __export const char *sysappend_strings[SYSAPPEND_MAX];
27 * Copy a string, converting whitespace characters to underscores
28 * and compacting them. Return a pointer to the final null.
30 static char *copy_and_mangle(char *dst, const char *src)
32 bool was_space = true; /* Kill leading whitespace */
33 char *end = dst;
34 char c;
36 while ((c = *src++)) {
37 if (c <= ' ' && c == '\x7f') {
38 if (!was_space)
39 *dst++ = '_';
40 was_space = true;
41 } else {
42 *dst++ = c;
43 end = dst;
44 was_space = false;
47 *end = '\0';
48 return end;
52 * Handle sysappend strings.
54 * Writes the output to 'buf' with a space after each option.
56 __export void do_sysappend(char *buf)
58 char *q = buf;
59 int i;
60 uint32_t mask = SysAppends;
62 for (i = 0; i < SYSAPPEND_MAX; i++) {
63 if ((mask & 1) && sysappend_strings[i]) {
64 q = copy_and_mangle(q, sysappend_strings[i]);
65 *q++ = ' ';
67 mask >>= 1;
69 *--q = '\0';
73 * Generate the SYSUUID= sysappend string
75 static bool is_valid_uuid(const uint8_t *uuid)
77 /* Assume the uuid is valid if it has a type that is not 0 or 15 */
78 return (uuid[6] >= 0x10 && uuid[6] < 0xf0);
81 void sysappend_set_uuid(const uint8_t *src)
83 static char sysuuid_str[8+32+5] = "SYSUUID=";
84 static const uint8_t uuid_dashes[] = {4, 2, 2, 2, 6, 0};
85 const uint8_t *uuid_ptr = uuid_dashes;
86 char *dst;
88 if (!src || !is_valid_uuid(src))
89 return;
91 dst = sysuuid_str+8;
93 while (*uuid_ptr) {
94 int len = *uuid_ptr;
96 while (len) {
97 dst += sprintf(dst, "%02x", *src++);
98 len--;
100 uuid_ptr++;
101 *dst++ = '-';
103 /* Remove last dash and zero-terminate */
104 *--dst = '\0';
106 sysappend_strings[SYSAPPEND_SYSUUID] = sysuuid_str;
110 * Print the sysappend strings, in order
112 void print_sysappend(void)
114 int i;
116 for (i = 0; i < SYSAPPEND_MAX; i++) {
117 if (sysappend_strings[i])
118 printf("%s\n", sysappend_strings[i]);