一种调试方法

芯片验证期间,可能在palladium上调试driver,早期只起了个ramdisk文件系统,网络,sd卡,u盘可能都不能使用,如何将ko传到文件系统里?如果把ko直接编译进内核,再用jtag烧录,这也是一种办法,但是由于palladium这个平台非常的慢,这样做每debug一次都会耗费1个多小时。
这里介绍另外一种方法,先将driver编译成ko,然后通过jtag讲ko下载到一段内核不用的内存空间里,再用一个小程序将内存里的数据读出来存成文件,然后insmod ko. jtag先将系统stop,然后下载ko,下载完毕后点击run,系统恢复后运行程序得到ko.实现了在系统运行时传递文件,不用每次debug都要下载重启,花费的时间也远小于jtag直接下载的方式.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <string.h>
#define MAP_MASK 4095UL
unsigned char buf[1024] = {0};
int main(int argc, char **argv)
{
    void *map_base, *virt_addr_start, *p_start;
    unsigned long file_size, size;
    unsigned long start;
    int fd_mem, fd_file;

    if (argc != 4) {
        printf("usage : ./get_file 0x33a00000 0x200000 hello\n");
        return -1;
    }

    start = strtol(argv[1], NULL, 16);
    file_size = strtol(argv[2], NULL, 16);
    printf("start : 0x%lx file_size :0x%lx \n", start, file_size);

    if ((fd_mem = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
        perror("open");
        return -1;
    }

    if ((fd_file = open(argv[3], O_RDWR | O_CREAT | O_TRUNC, 0777)) < 0) {
        perror("open");
        return -1;
    }
    map_base = mmap(0, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_mem, start & ~MAP_MASK);

    virt_addr_start = map_base + (start & MAP_MASK);
    p_start = virt_addr_start;
    size = 0;
    do {
        memcpy(buf, p_start, 1024);
        write(fd_file, buf, 1024);
        size += 1024;
        p_start += 1024;

    } while (size <= file_size );

    munmap(map_base, file_size);
    close(fd_mem);
    close(fd_file);
    return 0;
}

————————————————
版权声明:本文为CSDN博主「杨幂的咪」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Hsu_smile/article/details/85050321

发布了31 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Hsu_smile/article/details/103106697