1 /*- 2 * Copyright (c) 2016 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <stdint.h> 29 #include <string.h> 30 31 #include "endian.h" 32 #include "image.h" 33 #include "mkimg.h" 34 35 static void osdep_uuidgen(mkimg_uuid_t *); 36 37 #ifdef __APPLE__ 38 #include <uuid/uuid.h> 39 40 static void 41 osdep_uuidgen(mkimg_uuid_t *uuid) 42 { 43 44 uuid_generate_time((void *)uuid); 45 } 46 #endif /* __APPLE__ */ 47 48 #if defined(__linux__) || defined(__FreeBSD__) 49 #include <sys/time.h> 50 #include <stdlib.h> 51 52 static void 53 osdep_uuidgen(mkimg_uuid_t *uuid) 54 { 55 struct timeval tv; 56 uint64_t time = 0x01B21DD213814000LL; 57 u_int i; 58 uint16_t seq; 59 60 if (timestamp != (time_t)-1) { 61 tv.tv_sec = timestamp; 62 tv.tv_usec = 0; 63 } else if (gettimeofday(&tv, NULL) == -1) 64 abort(); 65 66 time += (uint64_t)tv.tv_sec * 10000000LL; 67 time += tv.tv_usec * 10; 68 69 uuid->time_low = (uint32_t)time; 70 uuid->time_mid = (uint16_t)(time >> 32); 71 uuid->time_hi_and_version = (uint16_t)(time >> 48) & 0xfff; 72 uuid->time_hi_and_version |= 1 << 12; 73 74 seq = random(); 75 76 uuid->clock_seq_hi_and_reserved = (uint8_t)(seq >> 8) & 0x3f; 77 uuid->clock_seq_low = (uint8_t)seq; 78 79 for (i = 0; i < 6; i++) 80 uuid->node[i] = (uint8_t)random(); 81 uuid->node[0] |= 0x01; 82 } 83 #endif /* __linux__ */ 84 85 void 86 mkimg_uuid(mkimg_uuid_t *uuid) 87 { 88 static uint8_t gen[sizeof(mkimg_uuid_t)]; 89 u_int i; 90 91 if (!unit_testing) { 92 osdep_uuidgen(uuid); 93 return; 94 } 95 96 for (i = 0; i < sizeof(gen); i++) 97 gen[i]++; 98 memcpy(uuid, gen, sizeof(*uuid)); 99 } 100 101 void 102 mkimg_uuid_enc(void *buf, const mkimg_uuid_t *uuid) 103 { 104 uint8_t *p = buf; 105 u_int i; 106 107 le32enc(p, uuid->time_low); 108 le16enc(p + 4, uuid->time_mid); 109 le16enc(p + 6, uuid->time_hi_and_version); 110 p[8] = uuid->clock_seq_hi_and_reserved; 111 p[9] = uuid->clock_seq_low; 112 for (i = 0; i < 6; i++) 113 p[10 + i] = uuid->node[i]; 114 } 115