142997114SMarcel Moolenaar /*-
242997114SMarcel Moolenaar * Copyright (c) 2016 Marcel Moolenaar
342997114SMarcel Moolenaar * All rights reserved.
442997114SMarcel Moolenaar *
542997114SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without
642997114SMarcel Moolenaar * modification, are permitted provided that the following conditions
742997114SMarcel Moolenaar * are met:
842997114SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright
942997114SMarcel Moolenaar * notice, this list of conditions and the following disclaimer.
1042997114SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright
1142997114SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the
1242997114SMarcel Moolenaar * documentation and/or other materials provided with the distribution.
1342997114SMarcel Moolenaar *
1442997114SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1542997114SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1642997114SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1742997114SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1842997114SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1942997114SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2042997114SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2142997114SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2242997114SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2342997114SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2442997114SMarcel Moolenaar * SUCH DAMAGE.
2542997114SMarcel Moolenaar */
2642997114SMarcel Moolenaar
2742997114SMarcel Moolenaar #include <sys/cdefs.h>
2842997114SMarcel Moolenaar #include <stdint.h>
2942997114SMarcel Moolenaar #include <string.h>
3042997114SMarcel Moolenaar
3142997114SMarcel Moolenaar #include "endian.h"
3242997114SMarcel Moolenaar #include "image.h"
3342997114SMarcel Moolenaar #include "mkimg.h"
3442997114SMarcel Moolenaar
3542997114SMarcel Moolenaar static void osdep_uuidgen(mkimg_uuid_t *);
3642997114SMarcel Moolenaar
3742997114SMarcel Moolenaar #ifdef __APPLE__
3842997114SMarcel Moolenaar #include <uuid/uuid.h>
3942997114SMarcel Moolenaar
4042997114SMarcel Moolenaar static void
osdep_uuidgen(mkimg_uuid_t * uuid)4142997114SMarcel Moolenaar osdep_uuidgen(mkimg_uuid_t *uuid)
4242997114SMarcel Moolenaar {
4342997114SMarcel Moolenaar
4442997114SMarcel Moolenaar uuid_generate_time((void *)uuid);
4542997114SMarcel Moolenaar }
4642997114SMarcel Moolenaar #endif /* __APPLE__ */
4742997114SMarcel Moolenaar
4842997114SMarcel Moolenaar #ifdef __FreeBSD__
4942997114SMarcel Moolenaar #include <sys/uuid.h>
5042997114SMarcel Moolenaar
5142997114SMarcel Moolenaar static void
osdep_uuidgen(mkimg_uuid_t * uuid)5242997114SMarcel Moolenaar osdep_uuidgen(mkimg_uuid_t *uuid)
5342997114SMarcel Moolenaar {
5442997114SMarcel Moolenaar
5542997114SMarcel Moolenaar uuidgen((void *)uuid, 1);
5642997114SMarcel Moolenaar }
5742997114SMarcel Moolenaar #endif /* __FreeBSD__ */
5842997114SMarcel Moolenaar
5942997114SMarcel Moolenaar #ifdef __linux__
6042997114SMarcel Moolenaar #include <stdlib.h>
6142997114SMarcel Moolenaar #include <time.h>
6242997114SMarcel Moolenaar
6342997114SMarcel Moolenaar static void
osdep_uuidgen(mkimg_uuid_t * uuid)6442997114SMarcel Moolenaar osdep_uuidgen(mkimg_uuid_t *uuid)
6542997114SMarcel Moolenaar {
66*baf4abfcSSimon J. Gerraty struct timeval tv;
6742997114SMarcel Moolenaar uint64_t time = 0x01B21DD213814000LL;
6842997114SMarcel Moolenaar u_int i;
6942997114SMarcel Moolenaar uint16_t seq;
7042997114SMarcel Moolenaar
71*baf4abfcSSimon J. Gerraty if (gettimeofday(&tv, NULL) == -1)
7242997114SMarcel Moolenaar abort();
7342997114SMarcel Moolenaar
74*baf4abfcSSimon J. Gerraty time += (uint64_t)tv.tv_sec * 10000000LL;
75*baf4abfcSSimon J. Gerraty time += tv.tv_usec * 10;
7642997114SMarcel Moolenaar
7742997114SMarcel Moolenaar uuid->time_low = (uint32_t)time;
7842997114SMarcel Moolenaar uuid->time_mid = (uint16_t)(time >> 32);
7942997114SMarcel Moolenaar uuid->time_hi_and_version = (uint16_t)(time >> 48) & 0xfff;
8042997114SMarcel Moolenaar uuid->time_hi_and_version |= 1 << 12;
8142997114SMarcel Moolenaar
8242997114SMarcel Moolenaar seq = random();
8342997114SMarcel Moolenaar
8442997114SMarcel Moolenaar uuid->clock_seq_hi_and_reserved = (uint8_t)(seq >> 8) & 0x3f;
8542997114SMarcel Moolenaar uuid->clock_seq_low = (uint8_t)seq;
8642997114SMarcel Moolenaar
8742997114SMarcel Moolenaar for (i = 0; i < 6; i++)
8842997114SMarcel Moolenaar uuid->node[i] = (uint8_t)random();
8942997114SMarcel Moolenaar uuid->node[0] |= 0x01;
9042997114SMarcel Moolenaar }
9142997114SMarcel Moolenaar #endif /* __linux__ */
9242997114SMarcel Moolenaar
9342997114SMarcel Moolenaar void
mkimg_uuid(mkimg_uuid_t * uuid)9442997114SMarcel Moolenaar mkimg_uuid(mkimg_uuid_t *uuid)
9542997114SMarcel Moolenaar {
9642997114SMarcel Moolenaar static uint8_t gen[sizeof(mkimg_uuid_t)];
9742997114SMarcel Moolenaar u_int i;
9842997114SMarcel Moolenaar
9942997114SMarcel Moolenaar if (!unit_testing) {
10042997114SMarcel Moolenaar osdep_uuidgen(uuid);
10142997114SMarcel Moolenaar return;
10242997114SMarcel Moolenaar }
10342997114SMarcel Moolenaar
10442997114SMarcel Moolenaar for (i = 0; i < sizeof(gen); i++)
10542997114SMarcel Moolenaar gen[i]++;
10642997114SMarcel Moolenaar memcpy(uuid, gen, sizeof(*uuid));
10742997114SMarcel Moolenaar }
10842997114SMarcel Moolenaar
10942997114SMarcel Moolenaar void
mkimg_uuid_enc(void * buf,const mkimg_uuid_t * uuid)11042997114SMarcel Moolenaar mkimg_uuid_enc(void *buf, const mkimg_uuid_t *uuid)
11142997114SMarcel Moolenaar {
11242997114SMarcel Moolenaar uint8_t *p = buf;
11342997114SMarcel Moolenaar u_int i;
11442997114SMarcel Moolenaar
11542997114SMarcel Moolenaar le32enc(p, uuid->time_low);
11642997114SMarcel Moolenaar le16enc(p + 4, uuid->time_mid);
11742997114SMarcel Moolenaar le16enc(p + 6, uuid->time_hi_and_version);
11842997114SMarcel Moolenaar p[8] = uuid->clock_seq_hi_and_reserved;
11942997114SMarcel Moolenaar p[9] = uuid->clock_seq_low;
12042997114SMarcel Moolenaar for (i = 0; i < 6; i++)
12142997114SMarcel Moolenaar p[10 + i] = uuid->node[i];
12242997114SMarcel Moolenaar }
123