[48674] | 1 | /* $Id: fsw_efi_lib.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * fsw_efi_lib.c - EFI host environment library functions.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[48674] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[48674] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[48674] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[48674] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[69498] | 35 | * ---------------------------------------------------------------------------
|
---|
[48674] | 36 | * This code is based on:
|
---|
| 37 | *
|
---|
| 38 | * Copyright (c) 2006 Christoph Pfisterer
|
---|
| 39 | *
|
---|
| 40 | * Redistribution and use in source and binary forms, with or without
|
---|
| 41 | * modification, are permitted provided that the following conditions are
|
---|
| 42 | * met:
|
---|
| 43 | *
|
---|
| 44 | * * Redistributions of source code must retain the above copyright
|
---|
| 45 | * notice, this list of conditions and the following disclaimer.
|
---|
| 46 | *
|
---|
| 47 | * * Redistributions in binary form must reproduce the above copyright
|
---|
| 48 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 49 | * documentation and/or other materials provided with the
|
---|
| 50 | * distribution.
|
---|
| 51 | *
|
---|
| 52 | * * Neither the name of Christoph Pfisterer nor the names of the
|
---|
| 53 | * contributors may be used to endorse or promote products derived
|
---|
| 54 | * from this software without specific prior written permission.
|
---|
| 55 | *
|
---|
| 56 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 57 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 58 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 59 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 60 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 61 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 62 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 63 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 64 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 65 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 66 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 67 | */
|
---|
| 68 |
|
---|
| 69 | #include "fsw_efi.h"
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | //
|
---|
| 73 | // time conversion
|
---|
| 74 | //
|
---|
| 75 | // Adopted from public domain code in FreeBSD libc.
|
---|
| 76 | //
|
---|
| 77 |
|
---|
| 78 | #define SECSPERMIN 60
|
---|
| 79 | #define MINSPERHOUR 60
|
---|
| 80 | #define HOURSPERDAY 24
|
---|
| 81 | #define DAYSPERWEEK 7
|
---|
| 82 | #define DAYSPERNYEAR 365
|
---|
| 83 | #define DAYSPERLYEAR 366
|
---|
| 84 | #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
|
---|
| 85 | #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
|
---|
| 86 | #define MONSPERYEAR 12
|
---|
| 87 |
|
---|
| 88 | #define EPOCH_YEAR 1970
|
---|
| 89 | #define EPOCH_WDAY TM_THURSDAY
|
---|
| 90 |
|
---|
| 91 | #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
|
---|
| 92 | #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
|
---|
| 93 |
|
---|
| 94 | static const int mon_lengths[2][MONSPERYEAR] = {
|
---|
| 95 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
---|
| 96 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
---|
| 97 | };
|
---|
| 98 | static const int year_lengths[2] = {
|
---|
| 99 | DAYSPERNYEAR, DAYSPERLYEAR
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | VOID fsw_efi_decode_time(OUT EFI_TIME *EfiTime, IN UINT32 UnixTime)
|
---|
| 103 | {
|
---|
| 104 | long days, rem;
|
---|
| 105 | int y, newy, yleap;
|
---|
| 106 | const int *ip;
|
---|
| 107 |
|
---|
| 108 | ZeroMem(EfiTime, sizeof(EFI_TIME));
|
---|
| 109 |
|
---|
| 110 | days = UnixTime / SECSPERDAY;
|
---|
| 111 | rem = UnixTime % SECSPERDAY;
|
---|
| 112 |
|
---|
| 113 | EfiTime->Hour = (UINT8) (rem / SECSPERHOUR);
|
---|
| 114 | rem = rem % SECSPERHOUR;
|
---|
| 115 | EfiTime->Minute = (UINT8) (rem / SECSPERMIN);
|
---|
| 116 | EfiTime->Second = (UINT8) (rem % SECSPERMIN);
|
---|
| 117 |
|
---|
| 118 | y = EPOCH_YEAR;
|
---|
| 119 | while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
|
---|
| 120 | newy = y + days / DAYSPERNYEAR;
|
---|
| 121 | if (days < 0)
|
---|
| 122 | --newy;
|
---|
| 123 | days -= (newy - y) * DAYSPERNYEAR +
|
---|
| 124 | LEAPS_THRU_END_OF(newy - 1) -
|
---|
| 125 | LEAPS_THRU_END_OF(y - 1);
|
---|
| 126 | y = newy;
|
---|
| 127 | }
|
---|
| 128 | EfiTime->Year = (UINT16)y;
|
---|
| 129 | ip = mon_lengths[yleap];
|
---|
| 130 | for (EfiTime->Month = 0; days >= (long) ip[EfiTime->Month]; ++(EfiTime->Month))
|
---|
| 131 | days = days - (long) ip[EfiTime->Month];
|
---|
| 132 | EfiTime->Month++; // adjust range to EFI conventions
|
---|
| 133 | EfiTime->Day = (UINT8) (days + 1);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | //
|
---|
| 137 | // String functions, used for file and volume info
|
---|
| 138 | //
|
---|
| 139 |
|
---|
| 140 | UINTN fsw_efi_strsize(struct fsw_string *s)
|
---|
| 141 | {
|
---|
| 142 | if (s->type == FSW_STRING_TYPE_EMPTY)
|
---|
| 143 | return sizeof(CHAR16);
|
---|
| 144 | return (s->len + 1) * sizeof(CHAR16);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | VOID fsw_efi_strcpy(CHAR16 *Dest, struct fsw_string *src)
|
---|
| 148 | {
|
---|
| 149 | if (src->type == FSW_STRING_TYPE_EMPTY) {
|
---|
| 150 | Dest[0] = 0;
|
---|
| 151 | } else if (src->type == FSW_STRING_TYPE_UTF16) {
|
---|
| 152 | CopyMem(Dest, src->data, src->size);
|
---|
| 153 | Dest[src->len] = 0;
|
---|
| 154 | } else {
|
---|
[69429] | 155 | /// @todo coerce, recurse
|
---|
[48674] | 156 | Dest[0] = 0;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | #ifdef VBOX
|
---|
| 161 | int fsw_streq_ISO88591_UTF16(void *s1data, void *s2data, int len)
|
---|
| 162 | {
|
---|
| 163 | int i;
|
---|
| 164 | fsw_u8 *p1 = (fsw_u8 *)s1data;
|
---|
| 165 | fsw_u16 *p2 = (fsw_u16 *)s2data;
|
---|
| 166 |
|
---|
| 167 | for (i = 0; i<len; i++)
|
---|
| 168 | {
|
---|
| 169 | if (fsw_to_lower(p1[i]) != fsw_to_lower(p2[i]))
|
---|
| 170 | {
|
---|
| 171 | return 0;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | return 1;
|
---|
| 176 | }
|
---|
| 177 | #endif
|
---|
| 178 |
|
---|
| 179 | // EOF
|
---|