15b090159SJason Zhu /* 25b090159SJason Zhu * Copyright (C) 2016 The Android Open Source Project 35b090159SJason Zhu * 45b090159SJason Zhu * Permission is hereby granted, free of charge, to any person 55b090159SJason Zhu * obtaining a copy of this software and associated documentation 65b090159SJason Zhu * files (the "Software"), to deal in the Software without 75b090159SJason Zhu * restriction, including without limitation the rights to use, copy, 85b090159SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 95b090159SJason Zhu * of the Software, and to permit persons to whom the Software is 105b090159SJason Zhu * furnished to do so, subject to the following conditions: 115b090159SJason Zhu * 125b090159SJason Zhu * The above copyright notice and this permission notice shall be 135b090159SJason Zhu * included in all copies or substantial portions of the Software. 145b090159SJason Zhu * 155b090159SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 165b090159SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 175b090159SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 185b090159SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 195b090159SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 205b090159SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 215b090159SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 225b090159SJason Zhu * SOFTWARE. 235b090159SJason Zhu */ 245b090159SJason Zhu 256c551138SJason Zhu #ifndef AVB_OPS_USER_H_ 266c551138SJason Zhu #define AVB_OPS_USER_H_ 276c551138SJason Zhu 286c551138SJason Zhu #include <android_avb/libavb.h> 296c551138SJason Zhu #include <android_avb/avb_ab_flow.h> 306c551138SJason Zhu 31*f74d184aSJason Zhu #define PERM_ATTR_DIGEST_SIZE 32 32*f74d184aSJason Zhu #define PERM_ATTR_TOTAL_SIZE 1084 33*f74d184aSJason Zhu 345b090159SJason Zhu /* Allocates an AvbOps instance suitable for use in Android userspace 355b090159SJason Zhu * on the device. Returns NULL on OOM. 365b090159SJason Zhu * 375b090159SJason Zhu * The returned AvbOps has the following characteristics: 385b090159SJason Zhu * 395b090159SJason Zhu * - The read_from_partition(), write_to_partition(), and 405b090159SJason Zhu * get_size_of_partition() operations are implemented, however for 415b090159SJason Zhu * these operations to work the fstab file on the device must have a 425b090159SJason Zhu * /misc entry using a by-name device file scheme and the containing 435b090159SJason Zhu * by-name/ subdirectory must have files for other partitions. 445b090159SJason Zhu * 455b090159SJason Zhu * - The remaining operations are implemented and never fails and 465b090159SJason Zhu * return the following values: 475b090159SJason Zhu * - validate_vbmeta_public_key(): always returns |true|. 485b090159SJason Zhu * - read_rollback_index(): returns 0 for any roolback index. 495b090159SJason Zhu * - write_rollback_index(): no-op. 505b090159SJason Zhu * - read_is_device_unlocked(): always returns |true|. 515b090159SJason Zhu * - get_unique_guid_for_partition(): always returns the empty string. 525b090159SJason Zhu * 535b090159SJason Zhu * - The |ab_ops| member will point to a valid AvbABOps instance 545b090159SJason Zhu * implemented via libavb_ab/. This should only be used if the AVB 555b090159SJason Zhu * A/B stack is used on the device. This is what is used in 565b090159SJason Zhu * bootctrl.avb boot control implementation. 575b090159SJason Zhu * 585b090159SJason Zhu * Free with avb_ops_user_free(). 595b090159SJason Zhu */ 606c551138SJason Zhu AvbOps* avb_ops_user_new(void); 615b090159SJason Zhu 625b090159SJason Zhu /* Frees an AvbOps instance previously allocated with avb_ops_device_new(). */ 636c551138SJason Zhu void avb_ops_user_free(AvbOps* ops); 645b090159SJason Zhu 655b090159SJason Zhu /** 665b090159SJason Zhu * Provided to fastboot to read how many slot in this system. 675b090159SJason Zhu * 685b090159SJason Zhu * @param slot_count We use parameter slot_count to obtain 695b090159SJason Zhu * how many slots in the system. 705b090159SJason Zhu * 715b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 725b090159SJason Zhu */ 735b090159SJason Zhu int avb_read_slot_count(char *slot_count); 745b090159SJason Zhu 755b090159SJason Zhu /** 765b090159SJason Zhu * The android things supply many slots, their name like '_a', '_b'. 775b090159SJason Zhu * We can use this function to read current slot is '_a' or '_b'. 785b090159SJason Zhu * 795b090159SJason Zhu * @slot_suffixes read value '_a' or '_b'. 805b090159SJason Zhu * 815b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 825b090159SJason Zhu */ 835b090159SJason Zhu int avb_read_slot_suffixes(char *slot_suffixes); 845b090159SJason Zhu 855b090159SJason Zhu /** 865b090159SJason Zhu * Use this function to set which slot boot first. 875b090159SJason Zhu * 885b090159SJason Zhu * @param slot_number set '0' or '1' 895b090159SJason Zhu * 905b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 915b090159SJason Zhu */ 925b090159SJason Zhu int avb_set_slot_active(unsigned int *slot_number); 935b090159SJason Zhu 945b090159SJason Zhu /** 955b090159SJason Zhu * Get current slot: '_a' or '_b'. 965b090159SJason Zhu * 975b090159SJason Zhu * @param select_slot obtain current slot. 985b090159SJason Zhu * 995b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1005b090159SJason Zhu */ 1015b090159SJason Zhu int avb_get_current_slot(char *select_slot); 1025b090159SJason Zhu 1035b090159SJason Zhu /** 1045b090159SJason Zhu * The android things defines permanent attributes to 1055b090159SJason Zhu * store PSK_public, product id. We can use this function 1065b090159SJason Zhu * to read them. 1075b090159SJason Zhu * 1085b090159SJason Zhu * @param attributes PSK_public, product id.... 1095b090159SJason Zhu * 1105b090159SJason Zhu * @param size The size of attributes. 1115b090159SJason Zhu * 1125b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1135b090159SJason Zhu */ 1145b090159SJason Zhu int avb_read_permanent_attributes(uint8_t *attributes, uint32_t size); 1155b090159SJason Zhu 1165b090159SJason Zhu /** 1175b090159SJason Zhu * The android things defines permanent attributes to 1185b090159SJason Zhu * store PSK_public, product id. We can use this function 1195b090159SJason Zhu * to write them. 1205b090159SJason Zhu * 1215b090159SJason Zhu * @param attributes PSK_public, product id.... 1225b090159SJason Zhu * 1235b090159SJason Zhu * @param size The size of attributes. 1245b090159SJason Zhu * 1255b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1265b090159SJason Zhu */ 1275b090159SJason Zhu int avb_write_permanent_attributes(uint8_t *attributes, uint32_t size); 1285b090159SJason Zhu 1295b090159SJason Zhu /** 1305b090159SJason Zhu * The funtion can be use to read the device state to judge 1315b090159SJason Zhu * whether the device can be flash. 1325b090159SJason Zhu * 1335b090159SJason Zhu * @param flash_lock_state A flag indicate the device flash state. 1345b090159SJason Zhu * 1355b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1365b090159SJason Zhu */ 1375b090159SJason Zhu int avb_read_flash_lock_state(uint8_t *flash_lock_state); 1385b090159SJason Zhu 1395b090159SJason Zhu /** 1405b090159SJason Zhu * The function is provided to write device flash state. 1415b090159SJason Zhu * 1425b090159SJason Zhu * @param flash_lock_state A flag indicate the device flash state. 1435b090159SJason Zhu * 1445b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1455b090159SJason Zhu */ 1465b090159SJason Zhu int avb_write_flash_lock_state(uint8_t flash_lock_state); 1475b090159SJason Zhu 1485b090159SJason Zhu /** 1495b090159SJason Zhu * The android things use the flag of lock state to indicate 1505b090159SJason Zhu * whether the device can be booted when verified error. 1515b090159SJason Zhu * 1525b090159SJason Zhu * @param lock_state A flag indicate the device lock state. 1535b090159SJason Zhu * 1545b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1555b090159SJason Zhu */ 1565b090159SJason Zhu int avb_read_lock_state(uint8_t *lock_state); 1575b090159SJason Zhu 1585b090159SJason Zhu /** 1595b090159SJason Zhu * The android things use the flag of lock state to indicate 1605b090159SJason Zhu * whether the device can be booted when verified error. 1615b090159SJason Zhu * 1625b090159SJason Zhu * @param lock_state A flag indicate the device lock state. 1635b090159SJason Zhu * 1645b090159SJason Zhu * @return 0 if the command succeeded, -1 if it failed 1655b090159SJason Zhu */ 1665b090159SJason Zhu int avb_write_lock_state(uint8_t lock_state); 1676c551138SJason Zhu 168*f74d184aSJason Zhu /** 169*f74d184aSJason Zhu * The android things uses fastboot to flash the permanent attributes. 170*f74d184aSJason Zhu * And if them were written, there must have a flag to indicate. 171*f74d184aSJason Zhu * 172*f74d184aSJason Zhu * @param flag indicate the permanent attributes have been written 173*f74d184aSJason Zhu * or not. 174*f74d184aSJason Zhu * 175*f74d184aSJason Zhu * @return 0 if the command succeeded, -1 if it failed 176*f74d184aSJason Zhu */ 177*f74d184aSJason Zhu int avb_read_perm_attr_flag(uint8_t *flag); 178*f74d184aSJason Zhu 179*f74d184aSJason Zhu /** 180*f74d184aSJason Zhu * The android things uses fastboot to flash the permanent attributes. 181*f74d184aSJason Zhu * And if them were written, there must have a flag to indicate. 182*f74d184aSJason Zhu * 183*f74d184aSJason Zhu * @param flag We can call this function to write the flag '1' 184*f74d184aSJason Zhu * to indicate the permanent attributes has been 185*f74d184aSJason Zhu * written. 186*f74d184aSJason Zhu * 187*f74d184aSJason Zhu * @return 0 if the command succeeded, -1 if it failed 188*f74d184aSJason Zhu */ 189*f74d184aSJason Zhu int avb_write_perm_attr_flag(uint8_t flag); 190*f74d184aSJason Zhu 1916c551138SJason Zhu #endif 192