本文主要是介绍android firmware下载机制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
加载固件的方法:
Linux 设备驱动的固件firmware加载
android 应用层
firmware文件要放入这三个目录之一:
/etc/firmware/
/vendor/firmware/
/firmware/image/
这里监听kernel发出的uevent(实际是监听netlink socket)。当收到uevent,根据名称在上面三个目录中寻找文件,找到文件后将文件传给kernel
android 中的ueventd是一个守护进程,主要作用是接收uevent来创建或删除/dev/xxx(设备节点),ueventd代码不多,下面我们直接针对代码分析
system/core/init/init.cpp
int main(int argc, char** argv) {if (!strcmp(basename(argv[0]), "ueventd")) {return ueventd_main(argc, argv);}
system/core/init/ueventd.cpp
int ueventd_main(int argc, char** argv) {/** init sets the umask to 077 for forked processes. We need to* create files with exact permissions, without modification by* the umask.*/umask(000);InitKernelLogging(argv);LOG(INFO) << "ueventd started!";selinux_callback cb;cb.func_log = selinux_klog_callback;selinux_set_callback(SELINUX_CB_LOG, cb);DeviceHandler device_handler = CreateDeviceHandler();UeventListener uevent_listener;if (access(COLDBOOT_DONE, F_OK) != 0) {ColdBoot cold_boot(uevent_listener, device_handler);cold_boot.Run();}// We use waitpid() in ColdBoot, so we can't ignore SIGCHLD until now.signal(SIGCHLD, SIG_IGN);// Reap and pending children that exited between the last call to waitpid() and setting SIG_IGN// for SIGCHLD above.while (waitpid(-1, nullptr, WNOHANG) > 0) {}uevent_listener.Poll([&device_handler](const Uevent& uevent) {HandleFirmwareEvent(uevent);device_handler.HandleDeviceEvent(uevent);return ListenerAction::kContinue;});return 0;
}
由HandleFirmwareEvent处理
system/core/init/firmware_handler.cpp
void HandleFirmwareEvent(const Uevent& uevent) {if (uevent.subsystem != "firmware" || uevent.action != "add") return;// Loading the firmware in a child means we can do that in parallel...auto pid = fork();if (pid == -1) {PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;}if (pid == 0) {Timer t;ProcessFirmwareEvent(uevent);LOG(INFO) << "loading " << uevent.path << " took " << t;_exit(EXIT_SUCCESS);}
}
static void ProcessFirmwareEvent(const Uevent& uevent) {int booting = IsBooting();LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";std::string root = "/sys" + uevent.path;std::string loading = root + "/loading";std::string data = root + "/data";unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));if (loading_fd == -1) {PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent.firmware;return;}unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));if (data_fd == -1) {PLOG(ERROR) << "couldn't open firmware data fd for " << uevent.firmware;return;}static const char* firmware_dirs[] = {"/etc/firmware/", "/vendor/firmware/","/firmware/image/"};try_loading_again:for (size_t i = 0; i < arraysize(firmware_dirs); i++) {std::string file = firmware_dirs[i] + uevent.firmware;unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));struct stat sb;if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {LoadFirmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);return;}}if (booting) {// If we're not fully booted, we may be missing// filesystems needed for firmware, wait and retry.std::this_thread::sleep_for(100ms);booting = IsBooting();goto try_loading_again;}LOG(ERROR) << "firmware: could not find firmware for " << uevent.firmware;// Write "-1" as our response to the kernel's firmware request, since we have nothing for it.write(loading_fd, "-1", 2);
}
由代码可知从
static const char* firmware_dirs[] = {"/etc/firmware/", “/vendor/firmware/”,
“/firmware/image/”};
load到/sys/class/firmware/data中
request_firmware(const struct firmware **firmware_p第一个参数返回在内核空间的buf地址
一般需要厂商校验成功后再写到模块
这篇关于android firmware下载机制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!