Skip to content

Instantly share code, notes, and snippets.

@cleverca22
Last active January 15, 2022 15:51
Show Gist options
  • Save cleverca22/58784f67690bfb97492f3f439ff00ed7 to your computer and use it in GitHub Desktop.
Save cleverca22/58784f67690bfb97492f3f439ff00ed7 to your computer and use it in GitHub Desktop.
rpi firmware load time
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;
}
@edo1
Copy link

edo1 commented Dec 26, 2021

can this program be adapted for rpi3?

@cleverca22
Copy link
Author

just change the peripheral base addr from 0xfe to 0x3f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment