当前位置: 首页 > news >正文

android 14 apexd分析(2)apexd 启动


1. class main进程一起启动,  apexservice是他提供的binderservice,这也第二阶段的最主要的作用
/system/apex/apexd/apexd.rc?r=3c8e8603c640fc41e0406ddcf981381803447cfb#1
1 service apexd /system/bin/apexd
2     interface aidl apexservice     ----------> aidl service
3     class core
4     user root
5     group system
6     oneshot
7     disabled # does not start with the core class
8     reboot_on_failure reboot,apexd-failed
9     # CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH required for apexdata snapshot & restore
10     # CAP_SYS_ADMIN is required to access device-mapper and to use mount syscall
11     capabilities CHOWN DAC_OVERRIDE DAC_READ_SEARCH FOWNER SYS_ADMIN

 
2. main()函数的的内容
 
/system/apex/apexd/apexd_main.cpp?r=3c8e8603c640fc41e0406ddcf981381803447cfb#106
106  int main(int /*argc*/, char** argv) {
.......
155    android::base::Result<android::apex::VoldCheckpointInterface>
156        vold_service_st = android::apex::VoldCheckpointInterface::Create();   ----> 获得Ivold binder service
157    android::apex::VoldCheckpointInterface* vold_service = nullptr;
158    if (!vold_service_st.ok()) {
159      LOG(ERROR) << "Could not retrieve vold service: "
160                 << vold_service_st.error();
161    } else {
162      vold_service = &*vold_service_st;
163    }
164    android::apex::Initialize(vold_service);                           ----> 获得使用Ivold的一些初始化操作
165  
166    if (booting) {         
167      if (auto res = android::apex::MigrateSessionsDirIfNeeded(); !res.ok()) {
168        LOG(ERROR) << "Failed to migrate sessions to /metadata partition : "
169                   << res.error();
170      }
171      android::apex::OnStart();                              ----> onstart 这其中就会 decompress compressedapex 和 ActivateApexPackages()
172    } else {
173      // TODO(b/172911822): Trying to use data apex related ApexFileRepository
174      //  apis without initializing it should throw error. Also, unit tests should
175      //  not pass without initialization.
176      // TODO(b/172911822): Consolidate this with Initialize() when
177      //  ApexFileRepository can act as cache and re-scanning is not expensive
178      android::apex::InitializeDataApex();
179    }
180    // start apexservice before ApexdLifecycle::WaitForBootStatus which waits for
181    // IApexService::markBootComplete().
182    android::apex::binder::CreateAndRegisterService();  ----> 把apexservice 添加到servicemanager中,这是个lazy service
183    android::apex::binder::StartThreadPool();
184  


题外话:
想要使用apexservice命令时,需要加上-w参数,因为他是个lazyservice,-w 是start and wait的意思,调用的是sm->waitforservice这个方法:
 

Example:
cmd -w apexservice getAllPackages
cmd -w apexservice deactivatePackage /system/apex/com.android.virt.apex
cmd -w apexservice activatePackage /system/apex/com.android.virt.apex

compressed apex解压如下:(实际上是对origin_apex又包了一层)
com.google.android.art_compressed$ ls
AndroidManifest.xml   apex_build_info.pb   apex_manifest.pb   apex_pubkey   META-INF   original_apex  original  stamp-cert-sha256

com.google.android.art_compressed/original_apex $ ls
AndroidManifest.xml  apex_build_info.pb  apex_manifest.pb  apex_payload.img  apex_pubkey  assets  META-INF  stamp-cert-sha256

3.ProcessCompressedApex() 把compressed apex搞到/data/apex/decompressed/下面
其中有一个验证比较有意思拉出来单聊,其他我也没兴趣,就没细看
 
2990  Result<void> ValidateDecompressedApex(const ApexFile& capex,
2991                                        const ApexFile& apex) {
2992    // Decompressed APEX must have same public key as CAPEX
2993    if (capex.GetBundledPublicKey() != apex.GetBundledPublicKey()) {=========> 1 压缩包的apexpubkey和original_apex的aepxpubkey是一样的
2994      return Error()
2995             << "Public key of compressed APEX is different than original "
2996             << "APEX for " << apex.GetPath();
2997    }
2998    // Decompressed APEX must have same version as CAPEX                        2 ======>压缩包的version和original_apex的version是一样的
2999    if (capex.GetManifest().version() != apex.GetManifest().version()) {
3000      return Error()
3001             << "Compressed APEX has different version than decompressed APEX "
3002             << apex.GetPath();
3003    }
3004    // Decompressed APEX must have same root digest as what is stored in CAPEX
3005    auto apex_verity = apex.VerifyApexVerity(apex.GetBundledPublicKey());      =======> 3 首先验证decompressed apex自己的avb特性
3006    if (!apex_verity.ok() ||
3007        capex.GetManifest().capexmetadata().originalapexdigest() !=
3008            apex_verity->root_digest) {
3009      return Error() << "Root digest of " << apex.GetPath()  =======> 4 再和compressed apex的manifest的root digest对比一下,见CompressedApexMetadata
3010                     << " does not match with"
3011                     << " expected root digest in " << capex.GetPath();
3012    }
3013    return {};
3014  }
秘钥的结果一模一样,就问你牛不牛逼
python3 avbtool.py info_image --image apex_payload.img
Footer version:           1.0
Image size:               831488 bytes
Original image size:      811008 bytes
VBMeta offset:            823296
VBMeta size:              2240 bytes
--
Minimum libavb version:   1.0
Header Block:             256 bytes
Authentication Block:     576 bytes
Auxiliary Block:          1408 bytes
Public key (sha1):        184474f2094c5dcbfc923af813bebddbf3f712e8
Algorithm:                SHA256_RSA4096
Rollback Index:           0
Flags:                    0
Rollback Index Location:  0
Release String:           'avbtool 1.2.0'
Descriptors:Hashtree descriptor: ------------------------------------- apex 用的是hashtreeVersion of dm-verity:  1Image Size:            811008 bytes                         -----> 198 blocksTree Offset:           811008Tree Size:             12288 bytes                          ------> 3 blocksData Block Size:       4096 bytesHash Block Size:       4096 bytesFEC num roots:         0FEC offset:            0FEC size:              0 bytesHash Algorithm:        sha256Partition Name:        Salt:                  b1c8aab92b646d2d85d0553fa1d3e0bea4c7289e2b740ae6079d40a490631bc0Root Digest:           25aaef0ecde6d8d66458d20f8ff4ef808c2630410443ffec18d82d8647aa332aFlags:                 0Prop: apex.key -> 'com_google_android_ipsec-image'      $ cat ./apex_pubkey |sha1sum
184474f2094c5dcbfc923af813bebddbf3f712e8hashtree的size的计算从layer 1开始,所以是3 blocks, 对于sha256算法,128个data block对应一个hashtree blocklayer 2                   root_hash_blk0/              \
layer 1         hash_blk_0              hash_blk_1/       \               /        \
layer 0    [blk_0] ....[blk_127]  [blk_128]....[blk_197]


4. ActivateApexPackages()  同上一章


5.PMS和apex的联系,用adb install apex
PMS在安装apex的时候,其实是通过apexservice 调用apexservice的
installAndActivatePackage()方法
/frameworks/base/services/core/java/com/android/server/pm/InstallingSession.java?r=3491936bce332909e2b20f7891ced303b9806925#495
495      private void processInstallRequests(boolean success, List<InstallRequest> installRequests) {
496          List<InstallRequest> apexInstallRequests = new ArrayList<>();
497          List<InstallRequest> apkInstallRequests = new ArrayList<>();
498          for (InstallRequest request : installRequests) {
499              if ((request.getInstallFlags() & PackageManager.INSTALL_APEX) != 0) {
500                  apexInstallRequests.add(request);
501              } else {
502                  apkInstallRequests.add(request);
503              }
504          }
505          // Note: supporting multi package install of both APEXes and APKs might requir some
506          // thinking to ensure atomicity of the install.
507          if (!apexInstallRequests.isEmpty() && !apkInstallRequests.isEmpty()) {
508              // This should've been caught at the validation step, but for some reason wasn't.
509              throw new IllegalStateException(
510                      "Attempted to do a multi package install of both APEXes and APKs");
511          }
512          if (!apexInstallRequests.isEmpty()) {
513              if (success) {
514                  // Since installApexPackages requires talking to external service (apexd), we
515                  // schedule to run it async. Once it finishes, it will resume the install.
516                  Thread t = new Thread(() -> installApexPackagesTraced(apexInstallRequests),  ====> 其实是调用的installAndActivatePackage()
517                          "installApexPackages");
518                  t.start();
519              } else {
520                  // Non-staged APEX installation failed somewhere before
521                  // processInstallRequestAsync. In that case just notify the observer about the
522                  // failure.
523                  InstallRequest request = apexInstallRequests.get(0);
524                  mPm.notifyInstallObserver(request);
525              }
526              return;
527          }
528   
529          processApkInstallRequests(success, installRequests);          ====> 普通apk的安装
530      }adb install apex命令和结果:
$ adb install com.android.ipsec@340818020.decompressed.apex
Performing Streamed Install
Success. Reboot device to apply staged session

http://www.lryc.cn/news/331320.html

相关文章:

  • 微信小程序怎么制作?制作一个微信小程序需要多少钱?
  • WPS二次开发专题:如何获取应用签名SHA256值
  • Flink SQL系列之:基于Flink SQL查询Topic中序列化的Debezium数据格式字段
  • 【WPF应用30】WPF中的ListBox控件详解
  • Chatgpt掘金之旅—有爱AI商业实战篇(二)
  • AGI时代,LLM可以在AutoML哪些环节进行增强?
  • 算法练习—day1
  • 关于ansible的模块 ③
  • Spring Boot--文件上传和下载
  • hexo博客7:构建简单的多层安全防御体系
  • 《捕鱼_ue4-5输出带技能的透明通道素材到AE步骤》
  • (免费分享)基于微信小程序自助停取车收费系统
  • Vue3_2024_7天【回顾上篇watch常见的后两种场景】___续
  • Gemini即将收费,GPT无需注册?GPT3.5白嫖和升级教程
  • 【协议篇:Http与Https】
  • WPS二次开发系列:WPS SDK初始化
  • EXCEL地理数据处理工具(地图任务)
  • 软件设计原则:迪米特法则
  • MongoDB聚合运算符:$max
  • 神经网络学习笔记10——RNN、ELMo、Transformer、GPT、BERT
  • Java23种设计模式
  • pieces of cake concerning torchtorchvision
  • 如何在Python中处理JSON数据?
  • 站群服务器如何提高搜索引擎排名
  • Redis安装-Docker
  • day16-二叉树part03
  • 上位机图像处理和嵌入式模块部署(qmacvisual亮度检测)
  • 防止推特Twitter账号被冻结,应该选什么代理类型IP?
  • 【二叉树】Leetcode 114. 二叉树展开为链表【中等】
  • 2024年150道高频Java面试题(二十)