Skip to content

Instantly share code, notes, and snippets.

@Maroc-OS
Forked from jungjoon/memedit.c
Created December 15, 2018 22:43
Show Gist options
  • Select an option

  • Save Maroc-OS/cd2829fb2f612e20c97ba4dc5a398408 to your computer and use it in GitHub Desktop.

Select an option

Save Maroc-OS/cd2829fb2f612e20c97ba4dc5a398408 to your computer and use it in GitHub Desktop.
for raw memory dump and edit via /dev/mem in linux
/*
for memory dump and edit via /dev/mem
** Note that any kernel constraints must be removed! ex, CONFIG_STRICT_DEVMEM and etc **
build intruction:
ANDROID_TARGET=android-8
CROSS_COMPILE=${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-
SYS_ROOT=${ANDROID_NDK}/platforms/${ANDROID_TARGET}/arch-arm/
${CROSS_COMPILE}gcc --sysroot=${SYS_ROOT} \
memedit.c \
-o memedit
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
int has_input;
unsigned int input;
char *ignored;
unsigned long offset;
int fd;
unsigned char* mem;
if (argc < 2) {
printf("Usage: %s <phys_addr> [input]\n", argv[0]);
return 0;
}
offset = strtoul(argv[1], &ignored, 16);
if (argc == 3) {
has_input = 1;
input = strtoul(argv[2], &ignored, 16);
} else {
has_input = 0;
input = 0;
}
fd = open("/dev/mem", O_SYNC | O_RDWR);
if (fd == 0) {
printf("Can't open /dev/mem\n");
return -1;
}
mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset & (~4095));
if (mem == NULL || mem == MAP_FAILED) {
printf("Can't map memory\n");
return -1;
}
printf("/dev/mem[0x%08x] = 0x%08x", offset, *(unsigned int*)&mem[offset & 4095]);
if (has_input) {
printf(" <= 0x%08x\n", input);
*((unsigned int*)&mem[offset & 4095]) = input;
}
else {
printf("\n");
}
close(fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment