1 /*
2 * Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef _PICO_BOOTROM_LOCK_H
8 #define _PICO_BOOTROM_LOCK_H
9
10 #include "hardware/boot_lock.h"
11 #include "pico/bootrom_constants.h"
12
13 // PICO_CONFIG: PICO_BOOTROM_LOCKING_ENABLED, Enable/disable locking for bootrom functions that use shared resources. If this flag is enabled bootrom lock checking is turned on and BOOT locks are taken around the relevant bootrom functions, type=bool, default=1, group=pico_bootrom
14 #ifndef PICO_BOOTROM_LOCKING_ENABLED
15 #if NUM_BOOT_LOCKS > 0
16 #define PICO_BOOTROM_LOCKING_ENABLED 1
17 #endif
18 #endif
19
20 /**
21 * \brief Try to acquire a bootrom lock
22 *
23 * If PICO_BOOTROM_LOCKING_ENABLED is false, this method returns true immediately
24 *
25 * \param lock_num the lock numbers - BOOTROM_LOCK_SHA_256, BOOTROM_LOCK_FLASH_OP or BOOTROM_LOCK_OTP
26 * \return true if the lock was acquired
27 */
bootrom_try_acquire_lock(uint lock_num)28 static inline bool bootrom_try_acquire_lock(uint lock_num) {
29 #if PICO_BOOTROM_LOCKING_ENABLED
30 // unsafe as this is a long term lock (so no irq disable)
31 return boot_try_lock_unsafe(boot_lock_instance(lock_num));
32 #else
33 (void)lock_num;
34 return true;
35 #endif
36 }
37
38 /**
39 * \brief Acquire a bootrom lock. If the lock is unavailable, block until it is available
40 *
41 * If PICO_BOOTROM_LOCKING_ENABLED is false, this method does nothing
42 *
43 * \param lock_num the lock numbers - BOOTROM_LOCK_SHA_256, BOOTROM_LOCK_FLASH_OP or BOOTROM_LOCK_OTP
44 */
bootrom_acquire_lock_blocking(uint lock_num)45 static inline void bootrom_acquire_lock_blocking(uint lock_num) {
46 #if PICO_BOOTROM_LOCKING_ENABLED
47 // unsafe as this is a long term lock (so no irq disable)
48 boot_lock_unsafe_blocking(boot_lock_instance(lock_num));
49 #else
50 (void)lock_num;
51 #endif
52 }
53
54 /**
55 * \brief Release a bootrom lock
56 *
57 * If PICO_BOOTROM_LOCKING_ENABLED is false, this method does nothing
58 *
59 * \param lock_num the lock numbers - BOOTROM_LOCK_SHA_256, BOOTROM_LOCK_FLASH_OP or BOOTROM_LOCK_OTP
60 */
bootrom_release_lock(uint lock_num)61 static inline void bootrom_release_lock(uint lock_num) {
62 #if PICO_BOOTROM_LOCKING_ENABLED
63 boot_unlock_unsafe(boot_lock_instance(lock_num));
64 #else
65 (void)lock_num;
66 #endif
67 }
68
69 #endif
70