ARMv8 的所有 OP-TEE 开发都是使用 C 完成的。但是,它缺乏主要的 libc 支持。实际上,它只有非常少的库(例如 string.h),这些库是从原始 libc 对应库中略去的版本。
这样,<time.h>
OP-TEE 中提供的内容只包含一个typedef
for time_t
,仅此而已。
问题可以分解为两个部分:
- 您如何获得自 1970 年 1 月 1 日 00:00:00 以来的时代?
这是一个有趣的问题,而直接的解决方案是简单地这样做:
TEE_Time tt;
TEE_GetREETime(&tt);
对于不想依赖 REE(Rich Execution Environment,AKA 易受攻击的环境)的时代计数的许多人来说,这种解决方案可能并不令人满意。对于安全敏感操作来说,这可能会带来问题,因为您需要时间来保持合法性,并且 REE 没有空间对其进行修改以执行特定攻击。
在上述情况下,您必须从硬件时钟获取历元,这取决于您正在开发 TA 的硬件板。您甚至可以从位置设备中检索它,这些设备也会在 NMEA 语句中返回 UTC 时间。虽然它可能不是 100% 精确到秒,但它可能就足够了。如果您需要非常高的精度,则需要从硬件设备安全地获取它。
无论哪种方式,您都必须弄清楚如何自己获得时代。这个答案侧重于第二部分:获取日期时间。
- 从时代获取日期时间。一旦您解决了第 1 步,您需要将其处理到日期时间。为此,您需要
gmtime
OP-TEE 中不存在的。它没有实现。你需要一个极简的实现来保持简单。
幸运的是,我能够找到这个答案。链接到newlib
为免费 BSD 开发的库,这些库非常适合嵌入式系统。因此为什么它在这里有用!
我能够从他们的实现中把它放在一起,你可以在这里使用它:
gmtime_r.h:
#include <inttypes.h>
#define SECSPERMIN 60L
#define MINSPERHOUR 60L
#define HOURSPERDAY 24L
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
#define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
#define DAYSPERWEEK 7
#define MONSPERYEAR 12
#define YEAR_BASE 1900
#define EPOCH_YEAR 1970
#define EPOCH_WDAY 4
#define EPOCH_YEARS_SINCE_LEAP 2
#define EPOCH_YEARS_SINCE_CENTURY 70
#define EPOCH_YEARS_SINCE_LEAP_CENTURY 370
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
typedef int64_t time_t;
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
struct tm* gmtime_r (time_t tim_p, struct tm* res);
gmtime_r.c:
#include "gmtime_r.h"
#define EPOCH_ADJUSTMENT_DAYS 719468L
/* year to which the adjustment was made */
#define ADJUSTED_EPOCH_YEAR 0
/* 1st March of year 0 is Wednesday */
#define ADJUSTED_EPOCH_WDAY 3
/* there are 97 leap years in 400-year periods. ((400 - 97) * 365 + 97 * 366) */
#define DAYS_PER_ERA 146097L
/* there are 24 leap years in 100-year periods. ((100 - 24) * 365 + 24 * 366) */
#define DAYS_PER_CENTURY 36524L
/* there is one leap year every 4 years */
#define DAYS_PER_4_YEARS (3 * 365 + 366)
/* number of days in a non-leap year */
#define DAYS_PER_YEAR 365
/* number of days in January */
#define DAYS_IN_JANUARY 31
/* number of days in non-leap February */
#define DAYS_IN_FEBRUARY 28
/* number of years per era */
#define YEARS_PER_ERA 400
struct tm* gmtime_r (time_t tim_p, struct tm* res)
{
time_t days, rem;
const time_t lcltime = tim_p;
int era, weekday, year;
unsigned erayear, yearday, month, day;
unsigned long eraday;
days = lcltime / SECSPERDAY + EPOCH_ADJUSTMENT_DAYS;
rem = lcltime % SECSPERDAY;
if (rem < 0)
{
rem += SECSPERDAY;
--days;
}
/* compute hour, min, and sec */
res->tm_hour = (int) (rem / SECSPERHOUR);
rem %= SECSPERHOUR;
res->tm_min = (int) (rem / SECSPERMIN);
res->tm_sec = (int) (rem % SECSPERMIN);
/* compute day of week */
if ((weekday = ((ADJUSTED_EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
weekday += DAYSPERWEEK;
res->tm_wday = weekday;
/* compute year, month, day & day of year */
/* for description of this algorithm see
* http://howardhinnant.github.io/date_algorithms.html#civil_from_days */
era = (days >= 0 ? days : days - (DAYS_PER_ERA - 1)) / DAYS_PER_ERA;
eraday = days - era * DAYS_PER_ERA; /* [0, 146096] */
erayear = (eraday - eraday / (DAYS_PER_4_YEARS - 1) + eraday / DAYS_PER_CENTURY -
eraday / (DAYS_PER_ERA - 1)) / 365; /* [0, 399] */
yearday = eraday - (DAYS_PER_YEAR * erayear + erayear / 4 - erayear / 100); /* [0, 365] */
month = (5 * yearday + 2) / 153; /* [0, 11] */
day = yearday - (153 * month + 2) / 5 + 1; /* [1, 31] */
month += month < 10 ? 2 : -10;
year = ADJUSTED_EPOCH_YEAR + erayear + era * YEARS_PER_ERA + (month <= 1);
res->tm_yday = yearday >= DAYS_PER_YEAR - DAYS_IN_JANUARY - DAYS_IN_FEBRUARY ?
yearday - (DAYS_PER_YEAR - DAYS_IN_JANUARY - DAYS_IN_FEBRUARY) :
yearday + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + isleap(erayear);
res->tm_year = year - YEAR_BASE;
res->tm_mon = month;
res->tm_mday = day;
res->tm_isdst = 0;
return (res);
}
您可以将这两个文件放在您的 TA 文件夹中,并确保gmtime.c
在sub.mk
. 最后,在 TA 本身中,您可以使用它:
TEE_Time tt;
TEE_GetREETime(&tt);
struct tm *lt, temp;
lt = gmtime((time_t)tt.seconds, &temp);
DMSG("%4d-%2d-%2d %2d:%2d:%2d", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
这将以正确的格式打印时间和日期。
目前,我还没有移植 的实现strftime
,但很快我也会这样做,这将自动格式化tm
结构并添加1900
totm_year
和1
to tm_mon
。
同时,我希望这能找到有需要的人。