boot_control.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <map>
  30. #include <list>
  31. #include <string>
  32. #include <vector>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include <errno.h>
  37. #define LOG_TAG "bootcontrolhal"
  38. #include <log/log.h>
  39. #include <hardware/boot_control.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <dirent.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <fcntl.h>
  47. #include <limits.h>
  48. #include <cutils/properties.h>
  49. #include "../recovery/gpt-utils/gpt-utils.h"
  50. #define BOOTDEV_DIR "/dev/block/bootdevice/by-name"
  51. #define BOOT_IMG_PTN_NAME "boot"
  52. #define LUN_NAME_END_LOC 14
  53. #define BOOT_SLOT_PROP "ro.boot.slot_suffix"
  54. #define SLOT_ACTIVE 1
  55. #define SLOT_INACTIVE 2
  56. #define UPDATE_SLOT(pentry, guid, slot_state) ({ \
  57. memcpy(pentry, guid, TYPE_GUID_SIZE); \
  58. if (slot_state == SLOT_ACTIVE)\
  59. *(pentry + AB_FLAG_OFFSET) = AB_SLOT_ACTIVE_VAL; \
  60. else if (slot_state == SLOT_INACTIVE) \
  61. *(pentry + AB_FLAG_OFFSET) = (*(pentry + AB_FLAG_OFFSET)& \
  62. ~AB_PARTITION_ATTR_SLOT_ACTIVE); \
  63. })
  64. using namespace std;
  65. const char *slot_suffix_arr[] = {
  66. AB_SLOT_A_SUFFIX,
  67. AB_SLOT_B_SUFFIX,
  68. NULL};
  69. enum part_attr_type {
  70. ATTR_SLOT_ACTIVE = 0,
  71. ATTR_BOOT_SUCCESSFUL,
  72. ATTR_UNBOOTABLE,
  73. };
  74. void boot_control_init(struct boot_control_module *module)
  75. {
  76. if (!module) {
  77. ALOGE("Invalid argument passed to %s", __func__);
  78. return;
  79. }
  80. return;
  81. }
  82. //Get the value of one of the attribute fields for a partition.
  83. static int get_partition_attribute(char *partname,
  84. enum part_attr_type part_attr)
  85. {
  86. struct gpt_disk *disk = NULL;
  87. uint8_t *pentry = NULL;
  88. int retval = -1;
  89. uint8_t *attr = NULL;
  90. if (!partname)
  91. goto error;
  92. disk = gpt_disk_alloc();
  93. if (!disk) {
  94. ALOGE("%s: Failed to alloc disk struct", __func__);
  95. goto error;
  96. }
  97. if (gpt_disk_get_disk_info(partname, disk)) {
  98. ALOGE("%s: Failed to get disk info", __func__);
  99. goto error;
  100. }
  101. pentry = gpt_disk_get_pentry(disk, partname, PRIMARY_GPT);
  102. if (!pentry) {
  103. ALOGE("%s: pentry does not exist in disk struct",
  104. __func__);
  105. goto error;
  106. }
  107. attr = pentry + AB_FLAG_OFFSET;
  108. if (part_attr == ATTR_SLOT_ACTIVE)
  109. retval = !!(*attr & AB_PARTITION_ATTR_SLOT_ACTIVE);
  110. else if (part_attr == ATTR_BOOT_SUCCESSFUL)
  111. retval = !!(*attr & AB_PARTITION_ATTR_BOOT_SUCCESSFUL);
  112. else if (part_attr == ATTR_UNBOOTABLE)
  113. retval = !!(*attr & AB_PARTITION_ATTR_UNBOOTABLE);
  114. else
  115. retval = -1;
  116. gpt_disk_free(disk);
  117. return retval;
  118. error:
  119. if (disk)
  120. gpt_disk_free(disk);
  121. return retval;
  122. }
  123. //Set a particular attribute for all the partitions in a
  124. //slot
  125. static int update_slot_attribute(const char *slot,
  126. enum part_attr_type ab_attr)
  127. {
  128. unsigned int i = 0;
  129. char buf[PATH_MAX];
  130. struct stat st;
  131. struct gpt_disk *disk = NULL;
  132. uint8_t *pentry = NULL;
  133. uint8_t *pentry_bak = NULL;
  134. int rc = -1;
  135. uint8_t *attr = NULL;
  136. uint8_t *attr_bak = NULL;
  137. char partName[MAX_GPT_NAME_SIZE + 1] = {0};
  138. const char ptn_list[][MAX_GPT_NAME_SIZE] = { AB_PTN_LIST };
  139. int slot_name_valid = 0;
  140. if (!slot) {
  141. ALOGE("%s: Invalid argument", __func__);
  142. goto error;
  143. }
  144. for (i = 0; slot_suffix_arr[i] != NULL; i++)
  145. {
  146. if (!strncmp(slot, slot_suffix_arr[i],
  147. strlen(slot_suffix_arr[i])))
  148. slot_name_valid = 1;
  149. }
  150. if (!slot_name_valid) {
  151. ALOGE("%s: Invalid slot name", __func__);
  152. goto error;
  153. }
  154. for (i=0; i < ARRAY_SIZE(ptn_list); i++) {
  155. memset(buf, '\0', sizeof(buf));
  156. //Check if A/B versions of this ptn exist
  157. snprintf(buf, sizeof(buf) - 1,
  158. "%s/%s%s",
  159. BOOT_DEV_DIR,
  160. ptn_list[i],
  161. AB_SLOT_A_SUFFIX
  162. );
  163. if (stat(buf, &st)) {
  164. //partition does not have _a version
  165. continue;
  166. }
  167. memset(buf, '\0', sizeof(buf));
  168. snprintf(buf, sizeof(buf) - 1,
  169. "%s/%s%s",
  170. BOOT_DEV_DIR,
  171. ptn_list[i],
  172. AB_SLOT_B_SUFFIX
  173. );
  174. if (stat(buf, &st)) {
  175. //partition does not have _a version
  176. continue;
  177. }
  178. memset(partName, '\0', sizeof(partName));
  179. snprintf(partName,
  180. sizeof(partName) - 1,
  181. "%s%s",
  182. ptn_list[i],
  183. slot);
  184. disk = gpt_disk_alloc();
  185. if (!disk) {
  186. ALOGE("%s: Failed to alloc disk struct",
  187. __func__);
  188. goto error;
  189. }
  190. rc = gpt_disk_get_disk_info(partName, disk);
  191. if (rc != 0) {
  192. ALOGE("%s: Failed to get disk info for %s",
  193. __func__,
  194. partName);
  195. goto error;
  196. }
  197. pentry = gpt_disk_get_pentry(disk, partName, PRIMARY_GPT);
  198. pentry_bak = gpt_disk_get_pentry(disk, partName, SECONDARY_GPT);
  199. if (!pentry || !pentry_bak) {
  200. ALOGE("%s: Failed to get pentry/pentry_bak for %s",
  201. __func__,
  202. partName);
  203. goto error;
  204. }
  205. attr = pentry + AB_FLAG_OFFSET;
  206. attr_bak = pentry_bak + AB_FLAG_OFFSET;
  207. if (ab_attr == ATTR_BOOT_SUCCESSFUL) {
  208. *attr = (*attr) | AB_PARTITION_ATTR_BOOT_SUCCESSFUL;
  209. *attr_bak = (*attr_bak) |
  210. AB_PARTITION_ATTR_BOOT_SUCCESSFUL;
  211. } else if (ab_attr == ATTR_UNBOOTABLE) {
  212. *attr = (*attr) | AB_PARTITION_ATTR_UNBOOTABLE;
  213. *attr_bak = (*attr_bak) | AB_PARTITION_ATTR_UNBOOTABLE;
  214. } else if (ab_attr == ATTR_SLOT_ACTIVE) {
  215. *attr = (*attr) | AB_PARTITION_ATTR_SLOT_ACTIVE;
  216. *attr_bak = (*attr) | AB_PARTITION_ATTR_SLOT_ACTIVE;
  217. } else {
  218. ALOGE("%s: Unrecognized attr", __func__);
  219. goto error;
  220. }
  221. if (gpt_disk_update_crc(disk)) {
  222. ALOGE("%s: Failed to update crc for %s",
  223. __func__,
  224. partName);
  225. goto error;
  226. }
  227. if (gpt_disk_commit(disk)) {
  228. ALOGE("%s: Failed to write back entry for %s",
  229. __func__,
  230. partName);
  231. goto error;
  232. }
  233. gpt_disk_free(disk);
  234. disk = NULL;
  235. }
  236. return 0;
  237. error:
  238. if (disk)
  239. gpt_disk_free(disk);
  240. return -1;
  241. }
  242. unsigned get_number_slots(struct boot_control_module *module)
  243. {
  244. struct dirent *de = NULL;
  245. DIR *dir_bootdev = NULL;
  246. unsigned slot_count = 0;
  247. if (!module) {
  248. ALOGE("%s: Invalid argument", __func__);
  249. goto error;
  250. }
  251. dir_bootdev = opendir(BOOTDEV_DIR);
  252. if (!dir_bootdev) {
  253. ALOGE("%s: Failed to open bootdev dir (%s)",
  254. __func__,
  255. strerror(errno));
  256. goto error;
  257. }
  258. while ((de = readdir(dir_bootdev))) {
  259. if (de->d_name[0] == '.')
  260. continue;
  261. if (!strncmp(de->d_name, BOOT_IMG_PTN_NAME,
  262. strlen(BOOT_IMG_PTN_NAME)))
  263. slot_count++;
  264. }
  265. closedir(dir_bootdev);
  266. return slot_count;
  267. error:
  268. if (dir_bootdev)
  269. closedir(dir_bootdev);
  270. return 0;
  271. }
  272. unsigned get_current_slot(struct boot_control_module *module)
  273. {
  274. uint32_t num_slots = 0;
  275. char bootSlotProp[PROPERTY_VALUE_MAX] = {'\0'};
  276. unsigned i = 0;
  277. if (!module) {
  278. ALOGE("%s: Invalid argument", __func__);
  279. goto error;
  280. }
  281. num_slots = get_number_slots(module);
  282. if (num_slots <= 1) {
  283. //Slot 0 is the only slot around.
  284. return 0;
  285. }
  286. property_get(BOOT_SLOT_PROP, bootSlotProp, "N/A");
  287. if (!strncmp(bootSlotProp, "N/A", strlen("N/A"))) {
  288. ALOGE("%s: Unable to read boot slot property",
  289. __func__);
  290. goto error;
  291. }
  292. //Iterate through a list of partitons named as boot+suffix
  293. //and see which one is currently active.
  294. for (i = 0; slot_suffix_arr[i] != NULL ; i++) {
  295. if (!strncmp(bootSlotProp,
  296. slot_suffix_arr[i],
  297. strlen(slot_suffix_arr[i])))
  298. return i;
  299. }
  300. error:
  301. //The HAL spec requires that we return a number between
  302. //0 to num_slots - 1. Since something went wrong here we
  303. //are just going to return the default slot.
  304. return 0;
  305. }
  306. static int boot_control_check_slot_sanity(struct boot_control_module *module,
  307. unsigned slot)
  308. {
  309. if (!module)
  310. return -1;
  311. uint32_t num_slots = get_number_slots(module);
  312. if ((num_slots < 1) || (slot > num_slots - 1)) {
  313. ALOGE("Invalid slot number");
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. int mark_boot_successful(struct boot_control_module *module)
  319. {
  320. unsigned cur_slot = 0;
  321. if (!module) {
  322. ALOGE("%s: Invalid argument", __func__);
  323. goto error;
  324. }
  325. cur_slot = get_current_slot(module);
  326. if (update_slot_attribute(slot_suffix_arr[cur_slot],
  327. ATTR_BOOT_SUCCESSFUL)) {
  328. goto error;
  329. }
  330. return 0;
  331. error:
  332. ALOGE("%s: Failed to mark boot successful", __func__);
  333. return -1;
  334. }
  335. const char *get_suffix(struct boot_control_module *module, unsigned slot)
  336. {
  337. if (boot_control_check_slot_sanity(module, slot) != 0)
  338. return NULL;
  339. else
  340. return slot_suffix_arr[slot];
  341. }
  342. //Return a gpt disk structure representing the disk that holds
  343. //partition.
  344. static struct gpt_disk* boot_ctl_get_disk_info(char *partition)
  345. {
  346. struct gpt_disk *disk = NULL;
  347. if (!partition)
  348. return NULL;
  349. disk = gpt_disk_alloc();
  350. if (!disk) {
  351. ALOGE("%s: Failed to alloc disk",
  352. __func__);
  353. goto error;
  354. }
  355. if (gpt_disk_get_disk_info(partition, disk)) {
  356. ALOGE("failed to get disk info for %s",
  357. partition);
  358. goto error;
  359. }
  360. return disk;
  361. error:
  362. if (disk)
  363. gpt_disk_free(disk);
  364. return NULL;
  365. }
  366. //The argument here is a vector of partition names(including the slot suffix)
  367. //that lie on a single disk
  368. static int boot_ctl_set_active_slot_for_partitions(vector<string> part_list,
  369. unsigned slot)
  370. {
  371. char buf[PATH_MAX] = {0};
  372. struct gpt_disk *disk = NULL;
  373. char slotA[MAX_GPT_NAME_SIZE + 1] = {0};
  374. char slotB[MAX_GPT_NAME_SIZE + 1] = {0};
  375. char active_guid[TYPE_GUID_SIZE + 1] = {0};
  376. char inactive_guid[TYPE_GUID_SIZE + 1] = {0};
  377. //Pointer to the partition entry of current 'A' partition
  378. uint8_t *pentryA = NULL;
  379. uint8_t *pentryA_bak = NULL;
  380. //Pointer to partition entry of current 'B' partition
  381. uint8_t *pentryB = NULL;
  382. uint8_t *pentryB_bak = NULL;
  383. struct stat st;
  384. vector<string>::iterator partition_iterator;
  385. for (partition_iterator = part_list.begin();
  386. partition_iterator != part_list.end();
  387. partition_iterator++) {
  388. //Chop off the slot suffix from the partition name to
  389. //make the string easier to work with.
  390. string prefix = *partition_iterator;
  391. if (prefix.size() < (strlen(AB_SLOT_A_SUFFIX) + 1)) {
  392. ALOGE("Invalid partition name: %s", prefix.c_str());
  393. goto error;
  394. }
  395. prefix.resize(prefix.size() - strlen(AB_SLOT_A_SUFFIX));
  396. //Check if A/B versions of this ptn exist
  397. snprintf(buf, sizeof(buf) - 1, "%s/%s%s", BOOT_DEV_DIR,
  398. prefix.c_str(),
  399. AB_SLOT_A_SUFFIX);
  400. if (stat(buf, &st))
  401. continue;
  402. memset(buf, '\0', sizeof(buf));
  403. snprintf(buf, sizeof(buf) - 1, "%s/%s%s", BOOT_DEV_DIR,
  404. prefix.c_str(),
  405. AB_SLOT_B_SUFFIX);
  406. if (stat(buf, &st))
  407. continue;
  408. memset(slotA, 0, sizeof(slotA));
  409. memset(slotB, 0, sizeof(slotA));
  410. snprintf(slotA, sizeof(slotA) - 1, "%s%s", prefix.c_str(),
  411. AB_SLOT_A_SUFFIX);
  412. snprintf(slotB, sizeof(slotB) - 1,"%s%s", prefix.c_str(),
  413. AB_SLOT_B_SUFFIX);
  414. //Get the disk containing the partitions that were passed in.
  415. //All partitions passed in must lie on the same disk.
  416. if (!disk) {
  417. disk = boot_ctl_get_disk_info(slotA);
  418. if (!disk)
  419. goto error;
  420. }
  421. //Get partition entry for slot A & B from the primary
  422. //and backup tables.
  423. pentryA = gpt_disk_get_pentry(disk, slotA, PRIMARY_GPT);
  424. pentryA_bak = gpt_disk_get_pentry(disk, slotA, SECONDARY_GPT);
  425. pentryB = gpt_disk_get_pentry(disk, slotB, PRIMARY_GPT);
  426. pentryB_bak = gpt_disk_get_pentry(disk, slotB, SECONDARY_GPT);
  427. if ( !pentryA || !pentryA_bak || !pentryB || !pentryB_bak) {
  428. //None of these should be NULL since we have already
  429. //checked for A & B versions earlier.
  430. ALOGE("Slot pentries for %s not found.",
  431. prefix.c_str());
  432. goto error;
  433. }
  434. memset(active_guid, '\0', sizeof(active_guid));
  435. memset(inactive_guid, '\0', sizeof(inactive_guid));
  436. if (get_partition_attribute(slotA, ATTR_SLOT_ACTIVE) == 1) {
  437. //A is the current active slot
  438. memcpy((void*)active_guid, (const void*)pentryA,
  439. TYPE_GUID_SIZE);
  440. memcpy((void*)inactive_guid,(const void*)pentryB,
  441. TYPE_GUID_SIZE);
  442. } else if (get_partition_attribute(slotB,
  443. ATTR_SLOT_ACTIVE) == 1) {
  444. //B is the current active slot
  445. memcpy((void*)active_guid, (const void*)pentryB,
  446. TYPE_GUID_SIZE);
  447. memcpy((void*)inactive_guid, (const void*)pentryA,
  448. TYPE_GUID_SIZE);
  449. } else {
  450. ALOGE("Both A & B are inactive..Aborting");
  451. goto error;
  452. }
  453. if (!strncmp(slot_suffix_arr[slot], AB_SLOT_A_SUFFIX,
  454. strlen(AB_SLOT_A_SUFFIX))){
  455. //Mark A as active in primary table
  456. UPDATE_SLOT(pentryA, active_guid, SLOT_ACTIVE);
  457. //Mark A as active in backup table
  458. UPDATE_SLOT(pentryA_bak, active_guid, SLOT_ACTIVE);
  459. //Mark B as inactive in primary table
  460. UPDATE_SLOT(pentryB, inactive_guid, SLOT_INACTIVE);
  461. //Mark B as inactive in backup table
  462. UPDATE_SLOT(pentryB_bak, inactive_guid, SLOT_INACTIVE);
  463. } else if (!strncmp(slot_suffix_arr[slot], AB_SLOT_B_SUFFIX,
  464. strlen(AB_SLOT_B_SUFFIX))){
  465. //Mark B as active in primary table
  466. UPDATE_SLOT(pentryB, active_guid, SLOT_ACTIVE);
  467. //Mark B as active in backup table
  468. UPDATE_SLOT(pentryB_bak, active_guid, SLOT_ACTIVE);
  469. //Mark A as inavtive in primary table
  470. UPDATE_SLOT(pentryA, inactive_guid, SLOT_INACTIVE);
  471. //Mark A as inactive in backup table
  472. UPDATE_SLOT(pentryA_bak, inactive_guid, SLOT_INACTIVE);
  473. } else {
  474. //Something has gone terribly terribly wrong
  475. ALOGE("%s: Unknown slot suffix!", __func__);
  476. goto error;
  477. }
  478. if (disk) {
  479. if (gpt_disk_update_crc(disk) != 0) {
  480. ALOGE("%s: Failed to update gpt_disk crc",
  481. __func__);
  482. goto error;
  483. }
  484. }
  485. }
  486. //write updated content to disk
  487. if (disk) {
  488. if (gpt_disk_commit(disk)) {
  489. ALOGE("Failed to commit disk entry");
  490. goto error;
  491. }
  492. gpt_disk_free(disk);
  493. }
  494. return 0;
  495. error:
  496. if (disk)
  497. gpt_disk_free(disk);
  498. return -1;
  499. }
  500. int set_active_boot_slot(struct boot_control_module *module, unsigned slot)
  501. {
  502. map<string, vector<string>> ptn_map;
  503. vector<string> ptn_vec;
  504. const char ptn_list[][MAX_GPT_NAME_SIZE] = { AB_PTN_LIST };
  505. uint32_t i;
  506. int rc = -1;
  507. int is_ufs = gpt_utils_is_ufs_device();
  508. map<string, vector<string>>::iterator map_iter;
  509. vector<string>::iterator string_iter;
  510. if (boot_control_check_slot_sanity(module, slot)) {
  511. ALOGE("%s: Bad arguments", __func__);
  512. goto error;
  513. }
  514. //The partition list just contains prefixes(without the _a/_b) of the
  515. //partitions that support A/B. In order to get the layout we need the
  516. //actual names. To do this we append the slot suffix to every member
  517. //in the list.
  518. for (i = 0; i < ARRAY_SIZE(ptn_list); i++) {
  519. //XBL is handled differrently for ufs devices so ignore it
  520. if (is_ufs && !strncmp(ptn_list[i], PTN_XBL, strlen(PTN_XBL)))
  521. continue;
  522. //The partition list will be the list of _a partitions
  523. string cur_ptn = ptn_list[i];
  524. cur_ptn.append(AB_SLOT_A_SUFFIX);
  525. ptn_vec.push_back(cur_ptn);
  526. }
  527. //The partition map gives us info in the following format:
  528. // [path_to_block_device_1]--><partitions on device 1>
  529. // [path_to_block_device_2]--><partitions on device 2>
  530. // ...
  531. // ...
  532. // eg:
  533. // [/dev/block/sdb]---><system, boot, rpm, tz,....>
  534. if (gpt_utils_get_partition_map(ptn_vec, ptn_map)) {
  535. ALOGE("%s: Failed to get partition map",
  536. __func__);
  537. goto error;
  538. }
  539. for (map_iter = ptn_map.begin(); map_iter != ptn_map.end(); map_iter++){
  540. if (map_iter->second.size() < 1)
  541. continue;
  542. boot_ctl_set_active_slot_for_partitions(map_iter->second, slot);
  543. }
  544. if (is_ufs) {
  545. if (!strncmp(slot_suffix_arr[slot], AB_SLOT_A_SUFFIX,
  546. strlen(AB_SLOT_A_SUFFIX))){
  547. //Set xbl_a as the boot lun
  548. rc = gpt_utils_set_xbl_boot_partition(NORMAL_BOOT);
  549. } else if (!strncmp(slot_suffix_arr[slot], AB_SLOT_B_SUFFIX,
  550. strlen(AB_SLOT_B_SUFFIX))){
  551. //Set xbl_b as the boot lun
  552. rc = gpt_utils_set_xbl_boot_partition(BACKUP_BOOT);
  553. } else {
  554. //Something has gone terribly terribly wrong
  555. ALOGE("%s: Unknown slot suffix!", __func__);
  556. goto error;
  557. }
  558. if (rc) {
  559. ALOGE("%s: Failed to switch xbl boot partition",
  560. __func__);
  561. goto error;
  562. }
  563. }
  564. return 0;
  565. error:
  566. return -1;
  567. }
  568. int set_slot_as_unbootable(struct boot_control_module *module, unsigned slot)
  569. {
  570. if (boot_control_check_slot_sanity(module, slot) != 0) {
  571. ALOGE("%s: Argument check failed", __func__);
  572. goto error;
  573. }
  574. if (update_slot_attribute(slot_suffix_arr[slot],
  575. ATTR_UNBOOTABLE)) {
  576. goto error;
  577. }
  578. return 0;
  579. error:
  580. ALOGE("%s: Failed to mark slot unbootable", __func__);
  581. return -1;
  582. }
  583. int is_slot_bootable(struct boot_control_module *module, unsigned slot)
  584. {
  585. int attr = 0;
  586. char bootPartition[MAX_GPT_NAME_SIZE + 1] = {0};
  587. if (boot_control_check_slot_sanity(module, slot) != 0) {
  588. ALOGE("%s: Argument check failed", __func__);
  589. goto error;
  590. }
  591. snprintf(bootPartition,
  592. sizeof(bootPartition) - 1, "boot%s",
  593. slot_suffix_arr[slot]);
  594. attr = get_partition_attribute(bootPartition, ATTR_UNBOOTABLE);
  595. if (attr >= 0)
  596. return !attr;
  597. error:
  598. return -1;
  599. }
  600. int is_slot_marked_successful(struct boot_control_module *module, unsigned slot)
  601. {
  602. int attr = 0;
  603. char bootPartition[MAX_GPT_NAME_SIZE + 1] = {0};
  604. if (boot_control_check_slot_sanity(module, slot) != 0) {
  605. ALOGE("%s: Argument check failed", __func__);
  606. goto error;
  607. }
  608. snprintf(bootPartition,
  609. sizeof(bootPartition) - 1,
  610. "boot%s", slot_suffix_arr[slot]);
  611. attr = get_partition_attribute(bootPartition, ATTR_BOOT_SUCCESSFUL);
  612. if (attr >= 0)
  613. return attr;
  614. error:
  615. return -1;
  616. }
  617. static hw_module_methods_t boot_control_module_methods = {
  618. .open = NULL,
  619. };
  620. boot_control_module_t HAL_MODULE_INFO_SYM = {
  621. .common = {
  622. .tag = HARDWARE_MODULE_TAG,
  623. .module_api_version = 1,
  624. .hal_api_version = 0,
  625. .id = BOOT_CONTROL_HARDWARE_MODULE_ID,
  626. .name = "Boot control HAL",
  627. .author = "Code Aurora Forum",
  628. .methods = &boot_control_module_methods,
  629. },
  630. .init = boot_control_init,
  631. .getNumberSlots = get_number_slots,
  632. .getCurrentSlot = get_current_slot,
  633. .markBootSuccessful = mark_boot_successful,
  634. .setActiveBootSlot = set_active_boot_slot,
  635. .setSlotAsUnbootable = set_slot_as_unbootable,
  636. .isSlotBootable = is_slot_bootable,
  637. .getSuffix = get_suffix,
  638. .isSlotMarkedSuccessful = is_slot_marked_successful,
  639. };
  640. #ifdef __cplusplus
  641. }
  642. #endif