Guest User

Untitled

a guest
Dec 15th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /*
  2. for memory dump and edit via /dev/mem
  3.  
  4. ** Note that any kernel constraints must be removed! ex, CONFIG_STRICT_DEVMEM and etc **
  5.  
  6. build intruction:
  7.  
  8. ANDROID_TARGET=android-8
  9. CROSS_COMPILE=${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-
  10. SYS_ROOT=${ANDROID_NDK}/platforms/${ANDROID_TARGET}/arch-arm/
  11.  
  12. ${CROSS_COMPILE}gcc --sysroot=${SYS_ROOT} \
  13. memedit.c \
  14. -o memedit
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <sys/mman.h>
  21.  
  22. int main(int argc, char *argv[]) {
  23. int has_input;
  24. unsigned int input;
  25. char *ignored;
  26. unsigned long offset;
  27. int fd;
  28. unsigned char* mem;
  29.  
  30. if (argc < 2) {
  31. printf("Usage: %s <phys_addr> [input]\n", argv[0]);
  32. return 0;
  33. }
  34.  
  35. offset = strtoul(argv[1], &ignored, 16);
  36.  
  37. if (argc == 3) {
  38. has_input = 1;
  39. input = strtoul(argv[2], &ignored, 16);
  40. } else {
  41. has_input = 0;
  42. input = 0;
  43. }
  44.  
  45. fd = open("/dev/mem", O_SYNC | O_RDWR);
  46.  
  47. if (fd == 0) {
  48. printf("Can't open /dev/mem\n");
  49. return -1;
  50. }
  51.  
  52. mem = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset & (~4095));
  53. if (mem == NULL || mem == MAP_FAILED) {
  54. printf("Can't map memory\n");
  55. return -1;
  56. }
  57.  
  58. printf("/dev/mem[0x%08x] = 0x%08x", offset, *(unsigned int*)&mem[offset & 4095]);
  59.  
  60. if (has_input) {
  61. printf(" <= 0x%08x\n", input);
  62. *((unsigned int*)&mem[offset & 4095]) = input;
  63. }
  64. else {
  65. printf("\n");
  66. }
  67.  
  68. close(fd);
  69. }
Add Comment
Please, Sign In to add comment