android 14 apexd分析(2)apexd 启动

发布于:2024-04-10 ⋅ 阅读:(151) ⋅ 点赞:(0)

 


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 用的是hashtree
      Version of dm-verity:  1
      Image Size:            811008 bytes                         -----> 198 blocks
      Tree Offset:           811008
      Tree Size:             12288 bytes                          ------> 3 blocks
      Data Block Size:       4096 bytes
      Hash Block Size:       4096 bytes
      FEC num roots:         0
      FEC offset:            0
      FEC size:              0 bytes
      Hash Algorithm:        sha256
      Partition Name:        
      Salt:                  b1c8aab92b646d2d85d0553fa1d3e0bea4c7289e2b740ae6079d40a490631bc0
      Root Digest:           25aaef0ecde6d8d66458d20f8ff4ef808c2630410443ffec18d82d8647aa332a
      Flags:                 0
    Prop: apex.key -> 'com_google_android_ipsec-image'      


$ cat ./apex_pubkey |sha1sum
184474f2094c5dcbfc923af813bebddbf3f712e8



hashtree的size的计算从layer 1开始,所以是3 blocks, 对于sha256算法,128个data block对应一个hashtree block

layer 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

 

 

 


网站公告

今日签到

点亮在社区的每一天
去签到