Skip to content

Instantly share code, notes, and snippets.

@waveform80
Forked from cleverca22/uptime.c
Last active September 17, 2020 12:11
Show Gist options
  • Save waveform80/b17a8452da40a9cbc38309960415c691 to your computer and use it in GitHub Desktop.
Save waveform80/b17a8452da40a9cbc38309960415c691 to your computer and use it in GitHub Desktop.
rpi firmware load time

From the original comment:

@Yoshqu there is a much simpler way to measure boot time to the usec, without having to use that custom bootloader

basically, ST_CLO begins counting at 1mhz, the instant the SoC comes out of reset and /proc/uptime begins counting seconds when linux starts up its internal clocks

the code in the gist, will then print both of them at once, and if you find the difference, youll see how long it took for linux to gain control of the core

root@raspberrypi:~# gcc uptime.c -o uptime && ./uptime
uptime.c: In function ‘main’:
uptime.c:11:11: warning: comparison between pointer and integer
if (addr == -1) {
^~
305.825899
4593.07 14749.79
root@raspberrypi:~# cat uptime.c
#include <stdint.h>
#include <sys/mman.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv) {
int fd = open("/dev/mem", O_RDONLY);
void *addr = mmap(NULL, 16 * 1024 * 1024, PROT_READ, MAP_SHARED, fd, 0xfe000000);
if (addr == -1) {
perror("unable to mmap");
return 1;
}
volatile uint32_t *st_clo = (volatile uint32_t*)(addr + 0x3004);
uint32_t snapshot = *st_clo;
printf("%3ld.%06ld\n", snapshot / 1000000, snapshot % 1000000);
munmap(addr, 16 * 1024 * 1024);
close(fd);
fd = open("/proc/uptime", O_RDONLY);
char buffer[1024];
int size = read(fd, buffer, 1024);
write(0, buffer, size);
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment