forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeson.build
More file actions
926 lines (807 loc) · 28.3 KB
/
meson.build
File metadata and controls
926 lines (807 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
project(
'wolfboot',
'c',
version: '2.5.0',
license: 'GPL-2.0-or-later',
meson_version: '>=1.3.0',
default_options: {
'c_std': 'gnu11',
'build.c_std': 'gnu11',
'warning_level': '2',
'buildtype': 'debugoptimized', # Default: -O2 -g for parent project compatibility
'default_library': 'static',
'b_ndebug': 'if-release',
'b_staticpic': false,
'b_lto': false,
},
)
# Include cortex CPU detection module
subdir('meson/cortex_detection')
# Compiler and filesystem helpers
cc = meson.get_compiler('c')
fs = import('fs')
# Basic configuration from options
arch = get_option('arch')
target = get_option('target')
sign = get_option('sign')
hash_alg = get_option('hash')
encrypt_enabled = get_option('encrypt')
# Include directories
inc_dirs = [
include_directories('.'),
include_directories('include'),
include_directories('lib/wolfssl'),
]
# Base compiler flags
c_args = [
'-D__WOLFBOOT',
'-DWOLFSSL_USER_SETTINGS',
'-Wall',
'-Wextra',
'-Wno-array-bounds',
'-Wno-main',
]
# Parent project compatibility mode - inherit optimization flags from parent
# wolfboot_debug option can override to force debug mode regardless of parent settings
current_buildtype = get_option('buildtype')
wolfboot_debug_enabled = get_option('wolfboot_debug')
if wolfboot_debug_enabled
# wolfboot_debug forces debug mode - override any optimization
c_args += ['-O0', '-g']
message('wolfBoot debug mode enabled: Using -O0 -g (overrides parent project optimization)')
elif get_option('parent_project_compat') and meson.is_subproject()
# When used as subproject, inherit parent's optimization flags
# Don't add explicit optimization flags - let parent project control them
message('Parent project compatibility mode: Inheriting optimization flags from parent project')
elif get_option('parent_project_compat') and current_buildtype != 'debug'
# Standalone build with parent compatibility - use -O2 as fallback
c_args += ['-O2']
message('Parent project compatibility mode (standalone): Using -O2 optimization')
else
# Use Meson's buildtype optimization settings
message('Using buildtype optimization: @0@'.format(current_buildtype))
endif
# Add debug symbols when built as a subproject (similar to library target in Makefile)
if meson.is_subproject()
c_args += ['-g']
c_args += ['-Ddebug=true']
message('Subproject detected: Adding debug symbols (-g)')
message('Subproject mode: Skipping HAL files, executables, and flash drivers - exposing libraries, dependencies, and tools')
endif
# wolfBoot debug configuration - only add debug defines if not already handled above
if get_option('wolfboot_debug')
c_args += ['-DDEBUG', '-DWOLFBOOT_DEBUG_ENABLED']
# Also add as project-wide argument to ensure it gets applied
add_project_arguments(['-DDEBUG', '-DWOLFBOOT_DEBUG_ENABLED'], language: 'c')
message('wolfBoot debug defines enabled')
endif
# Small stack configuration
if get_option('wolfboot_small_stack')
c_args += ['-DWOLFBOOT_SMALL_STACK', '-DXMALLOC_USER']
endif
# Critical system configuration flags
if not get_option('vtor')
c_args += ['-DNO_VTOR']
add_project_arguments(['-DNO_VTOR'], language: 'c')
endif
if get_option('no_mpu')
c_args += ['-DWOLFBOOT_NO_MPU']
add_project_arguments(['-DWOLFBOOT_NO_MPU'], language: 'c')
endif
if get_option('ram_code')
c_args += ['-DRAM_CODE']
add_project_arguments(['-DRAM_CODE'], language: 'c')
endif
if get_option('dualbank_swap')
c_args += ['-DDUALBANK_SWAP=1']
add_project_arguments(['-DDUALBANK_SWAP=1'], language: 'c')
endif
if get_option('allow_downgrade')
c_args += ['-DALLOW_DOWNGRADE']
add_project_arguments(['-DALLOW_DOWNGRADE'], language: 'c')
endif
if get_option('nvm_flash_writeonce')
c_args += ['-DNVM_FLASH_WRITEONCE']
add_project_arguments(['-DNVM_FLASH_WRITEONCE'], language: 'c')
endif
if get_option('disable_backup')
c_args += ['-DDISABLE_BACKUP']
add_project_arguments(['-DDISABLE_BACKUP'], language: 'c')
endif
if get_option('flags_home')
c_args += ['-DFLAGS_HOME=1']
add_project_arguments(['-DFLAGS_HOME=1'], language: 'c')
endif
if get_option('flags_invert')
c_args += ['-DWOLFBOOT_FLAGS_INVERT=1']
add_project_arguments(['-DWOLFBOOT_FLAGS_INVERT=1'], language: 'c')
endif
wolfboot_version = get_option('wolfboot_version')
if wolfboot_version != 0
c_args += ['-DWOLFBOOT_VERSION=@0@'.format(wolfboot_version)]
endif
# ARM TrustZone configuration
if get_option('tzen')
c_args += ['-DTZEN']
endif
if get_option('wolfcrypt_tz')
c_args += ['-DWOLFCRYPT_SECURE_MODE']
endif
if get_option('wolfcrypt_tz_pkcs11')
c_args += ['-DSECURE_PKCS11', '-DWOLFSSL_PKCS11_RW_TOKENS']
c_args += ['-DCK_CALLABLE="__attribute__((cmse_nonsecure_entry))"']
c_args += ['-DWP11_HASH_PIN_COST=3']
endif
# Hardware acceleration and platform features
if get_option('pka')
c_args += ['-DPKA=1']
endif
if get_option('psoc6_crypto')
c_args += ['-DPSOC6_CRYPTO=1']
endif
if get_option('armored')
c_args += ['-DWOLFBOOT_ARMORED']
endif
if get_option('wolfboot_huge_stack')
c_args += ['-DWOLFBOOT_HUGE_STACK']
endif
if get_option('no_xip')
c_args += ['-DNO_XIP']
endif
# Advanced features
if get_option('elf')
c_args += ['-DWOLFBOOT_ELF']
endif
if get_option('multiboot2')
c_args += ['-DWOLFBOOT_MULTIBOOT2']
endif
if get_option('linux_payload')
c_args += ['-DWOLFBOOT_LINUX_PAYLOAD']
endif
if get_option('64bit')
c_args += ['-DWOLFBOOT_64BIT']
endif
if get_option('wolfboot_universal_keystore')
c_args += ['-DWOLFBOOT_UNIVERSAL_KEYSTORE']
endif
if get_option('disk_lock')
c_args += ['-DWOLFBOOT_ATA_DISK_LOCK']
disk_lock_password = get_option('disk_lock_password')
if disk_lock_password != ''
c_args += ['-DWOLFBOOT_ATA_DISK_LOCK_PASSWORD="@0@"'.format(disk_lock_password)]
endif
endif
if get_option('force_32bit')
c_args += ['-DFORCE_32BIT']
endif
if get_option('big_endian')
c_args += ['-DBIG_ENDIAN=1']
endif
if get_option('flash_multi_sector_erase')
c_args += ['-DWOLFBOOT_FLASH_MULTI_SECTOR_ERASE']
endif
# wolfHSM support
if get_option('wolfhsm_client')
c_args += ['-DWOLFHSM_CLIENT=1']
if get_option('wolfhsm_client_local_keys')
c_args += ['-DWOLFHSM_CLIENT_LOCAL_KEYS=1']
endif
endif
# Secondary signature support
sign_secondary = get_option('sign_secondary')
if sign_secondary != ''
c_args += ['-DSIGN_HYBRID', '-DWOLFBOOT_SIGN_SECONDARY_@0@'.format(sign_secondary)]
endif
# LMS/XMSS/ML-DSA parameters
if sign == 'LMS'
lms_levels = get_option('lms_levels')
lms_height = get_option('lms_height')
lms_winternitz = get_option('lms_winternitz')
c_args += [
'-DLMS_LEVELS=@0@'.format(lms_levels),
'-DLMS_HEIGHT=@0@'.format(lms_height),
'-DLMS_WINTERNITZ=@0@'.format(lms_winternitz)
]
endif
if sign == 'XMSS'
xmss_params = get_option('xmss_params')
c_args += ['-DWOLFBOOT_XMSS_PARAMS="@0@"'.format(xmss_params)]
endif
if sign == 'ML_DSA'
ml_dsa_level = get_option('ml_dsa_level')
c_args += ['-DML_DSA_LEVEL=@0@'.format(ml_dsa_level)]
endif
# Architecture-specific configuration
if arch == 'ARM'
c_args += ['-DARCH_ARM']
if get_option('use_gcc_headless')
c_args += [
'-ffreestanding',
'-ffunction-sections',
'-fdata-sections',
'-fomit-frame-pointer',
]
endif
# Use shared Cortex-M CPU detection
cortex_cpu = cortex_cpu_detected
# Set Cortex-M specific flags based on detected CPU
if cortex_cpu.contains('cortex-m0')
c_args += ['-DCORTEX_M0']
message('Auto-detected Cortex-M0 processor')
elif cortex_cpu.contains('cortex-m3')
c_args += ['-DCORTEX_M3']
message('Auto-detected Cortex-M3 processor')
elif cortex_cpu.contains('cortex-m7')
c_args += ['-DCORTEX_M7']
message('Auto-detected Cortex-M7 processor')
elif cortex_cpu.contains('cortex-m33') or cortex_cpu.contains('cortex-m35')
c_args += ['-DCORTEX_M33']
message('Auto-detected Cortex-M33/M35 processor')
elif cortex_cpu.contains('cortex-m4')
c_args += ['-DCORTEX_M4']
message('Auto-detected Cortex-M4 processor')
elif cortex_cpu.contains('cortex-m23')
c_args += ['-DCORTEX_M23']
message('Auto-detected Cortex-M23 processor')
elif cortex_cpu != ''
message('Detected ARM CPU: @0@ (no specific Cortex-M optimizations applied)'.format(cortex_cpu))
else
message('ARM architecture detected but no specific Cortex-M CPU identified')
endif
elif arch == 'AARCH64'
c_args += ['-DARCH_AARCH64']
elif arch == 'x86_64'
c_args += ['-DARCH_x86_64', '-DFAST_MEMCPY']
endif
# Target-specific configuration
flash_offset = get_option('arch_flash_offset')
c_args += ['-DARCH_FLASH_OFFSET=@0@'.format(flash_offset)]
# wolfBoot partition addresses and sizes
wolfboot_partition_size = get_option('wolfboot_partition_size')
wolfboot_partition_boot_address = get_option('wolfboot_partition_boot_address')
wolfboot_partition_update_address = get_option('wolfboot_partition_update_address')
wolfboot_partition_swap_address = get_option('wolfboot_partition_swap_address')
wolfboot_sector_size = get_option('wolfboot_sector_size')
# Convert hex strings to integers for calculations
# Use a simple approach: define bootloader partition size directly
c_args += ['-DBOOTLOADER_PARTITION_SIZE=0x20000'] # Default size for STM32H7
# Signature algorithm configuration
if sign != 'NONE'
if sign == 'ECC256'
c_args += ['-DWOLFBOOT_SIGN_ECC256']
image_signature_size = 64
elif sign == 'ECC384'
c_args += ['-DWOLFBOOT_SIGN_ECC384']
image_signature_size = 96
elif sign == 'ECC521'
c_args += ['-DWOLFBOOT_SIGN_ECC521']
image_signature_size = 132
elif sign == 'ED25519'
c_args += ['-DWOLFBOOT_SIGN_ED25519']
image_signature_size = 64
elif sign == 'ED448'
c_args += ['-DWOLFBOOT_SIGN_ED448']
image_signature_size = 114
elif sign.startswith('RSA')
c_args += ['-DWOLFBOOT_SIGN_RSA']
if sign == 'RSA2048'
c_args += ['-DWOLFBOOT_SIGN_RSA2048']
image_signature_size = 256
elif sign == 'RSA3072'
c_args += ['-DWOLFBOOT_SIGN_RSA3072']
image_signature_size = 384
elif sign == 'RSA4096'
c_args += ['-DWOLFBOOT_SIGN_RSA4096']
image_signature_size = 512
endif
endif
else
image_signature_size = 0
endif
# Hash algorithm configuration
if hash_alg == 'SHA256'
c_args += ['-DWOLFBOOT_HASH_SHA256']
elif hash_alg == 'SHA384'
c_args += ['-DWOLFBOOT_HASH_SHA384']
elif hash_alg == 'SHA3'
c_args += ['-DWOLFBOOT_HASH_SHA3']
endif
# Image header size
image_header_size = get_option('image_header_size')
c_args += ['-DIMAGE_HEADER_SIZE=@0@'.format(image_header_size)]
c_args += ['-DIMAGE_SIGNATURE_SIZE=@0@'.format(image_signature_size)]
# Build wolfcrypt library from lib subdirectory
subdir('lib')
# Get the wolfcrypt dependency from the subdirectory
wolfcrypt_dep = get_variable('wolfcrypt_dep')
# Create additional include directory for generated headers
# This allows subprojects to find generated target.h when available
# Note: Using '.' includes both source and build directories automatically
generated_inc = declare_dependency(
include_directories: include_directories('.', is_system: false)
)
# Core wolfBoot sources
wolfboot_sources = [
'src/string.c',
'src/image.c',
'src/libwolfboot.c',
'src/update_flash.c',
]
# Add xmalloc if small stack is enabled
if get_option('wolfboot_small_stack')
wolfboot_sources += ['src/xmalloc.c']
endif
# Add delta updates support
if get_option('delta_updates')
wolfboot_sources += ['src/delta.c']
endif
# Add ELF loader support
if get_option('elf')
wolfboot_sources += ['src/elf.c']
endif
# Add multiboot support
if get_option('multiboot2')
wolfboot_sources += ['src/multiboot.c']
endif
# Add disk lock support
if get_option('disk_lock')
# Note: This would require additional wolfSSL coding sources
# wolfboot_sources += ['lib/wolfssl/wolfcrypt/src/coding.c']
endif
# Add PKCS11 and TrustZone sources
if get_option('wolfcrypt_tz_pkcs11')
wolfboot_sources += [
'src/pkcs11_store.c',
'src/pkcs11_callable.c'
]
endif
# Add TrustZone callable functions
if get_option('wolfcrypt_tz')
wolfboot_sources += ['src/wc_callable.c']
endif
# Add TPM support sources
if get_option('wolfboot_tpm_verify') or get_option('measured_boot') or get_option('wolfboot_tpm_keystore') or get_option('wolfboot_tpm_seal')
wolfboot_sources += ['src/tpm.c']
# Add TPM settings only when TPM features are enabled
c_args += ['-DWOLFTPM_USER_SETTINGS']
endif
# Add architecture-specific boot file for ARM targets
if arch in ['ARM', 'ARM_BE'] or target in ['stm32f4', 'stm32f7', 'stm32h7', 'stm32l0', 'stm32l4', 'stm32l5', 'stm32u5', 'stm32h5', 'stm32wb', 'stm32g0', 'stm32c0', 'nrf52', 'nrf5340', 'kinetis', 'mcxa', 'mcxw', 'lpc', 'same51', 'samr21', 'sama5d3', 'pic32c', 'pic32ck', 'pic32cz', 'psoc6']
wolfboot_sources += ['src/boot_arm.c']
elif arch == 'AARCH64'
wolfboot_sources += ['src/boot_aarch64.c', 'src/boot_aarch64_start.c']
elif arch == 'x86_64'
if target == 'x86_64_efi'
wolfboot_sources += ['src/boot_x86_64.c']
endif
elif arch == 'RISCV'
wolfboot_sources += ['src/boot_riscv.c']
elif arch == 'PPC'
wolfboot_sources += ['src/boot_ppc.c', 'src/boot_ppc_start.c']
elif arch == 'RENESAS_RX'
wolfboot_sources += ['src/boot_renesas.c', 'src/boot_renesas_start.c']
endif
# Add loader unless disabled
if not get_option('no_loader')
wolfboot_sources += ['src/loader.c']
endif
# HAL sources
hal_sources = ['hal/hal.c']
# Add target-specific HAL unless building library target or when used as subproject
if target != 'library' and not meson.is_subproject()
hal_sources += ['hal/@0@.c'.format(target)]
endif
# Add TrustZone specific HAL for STM32 targets
if get_option('tzen') and target.startswith('stm32') and not meson.is_subproject()
hal_sources += ['hal/stm32_tz.c']
endif
# Add architecture-specific HAL files
if arch == 'RENESAS_RX' and not meson.is_subproject()
hal_sources += ['hal/renesas-rx.c']
endif
# Flash driver sources based on options
if get_option('spi_flash') and not meson.is_subproject()
hal_sources += ['src/spi_flash.c']
spi_target = get_option('spi_target')
if fs.exists('hal/spi/spi_drv_@0@.c'.format(spi_target))
hal_sources += ['hal/spi/spi_drv_@0@.c'.format(spi_target)]
endif
endif
if get_option('qspi_flash') and not meson.is_subproject()
hal_sources += ['src/qspi_flash.c']
endif
if get_option('uart_flash') and not meson.is_subproject()
hal_sources += ['src/uart_flash.c']
uart_target = get_option('uart_target')
if fs.exists('hal/uart/uart_drv_@0@.c'.format(uart_target))
hal_sources += ['hal/uart/uart_drv_@0@.c'.format(uart_target)]
endif
endif
# Build HAL library (skip when subproject or library target)
if target != 'library' and not meson.is_subproject()
libwolfboothal = static_library(
'wolfboothal',
hal_sources,
include_directories: inc_dirs,
c_args: c_args,
dependencies: generated_inc, # Include generated headers
install: false,
)
endif
# Public key sources - will be updated after keystore generation if needed
public_key_sources = []
if sign != 'NONE'
if get_option('flash_otp_keystore')
public_key_sources += ['src/flash_otp_keystore.c']
else
# keystore.c will be generated by keygen tool
# Don't add it here - it will be handled in the generation section
endif
endif
# Collect flash driver sources for libwolfboot (similar to WOLFBOOT_FLASH_SOURCES in CMake)
# Skip flash drivers when used as subproject unless specifically needed
flash_sources = []
if get_option('spi_flash') and not meson.is_subproject()
flash_sources += ['src/spi_flash.c']
spi_target = get_option('spi_target')
if fs.exists('hal/spi/spi_drv_@0@.c'.format(spi_target))
flash_sources += ['hal/spi/spi_drv_@0@.c'.format(spi_target)]
endif
endif
if get_option('qspi_flash') and not meson.is_subproject()
flash_sources += ['src/qspi_flash.c']
endif
if get_option('uart_flash') and not meson.is_subproject()
flash_sources += ['src/uart_flash.c']
uart_target = get_option('uart_target')
if fs.exists('hal/uart/uart_drv_@0@.c'.format(uart_target))
flash_sources += ['hal/uart/uart_drv_@0@.c'.format(uart_target)]
endif
endif
# Libraries will be built after keystore generation to resolve dependencies
# Generate target.h configuration file
target_h_data = configuration_data()
target_h_data.set('WOLFBOOT_SECTOR_SIZE', wolfboot_sector_size)
target_h_data.set('WOLFBOOT_PARTITION_BOOT_ADDRESS', wolfboot_partition_boot_address)
target_h_data.set('WOLFBOOT_PARTITION_UPDATE_ADDRESS', wolfboot_partition_update_address)
target_h_data.set('WOLFBOOT_PARTITION_SWAP_ADDRESS', wolfboot_partition_swap_address)
target_h_data.set('WOLFBOOT_PARTITION_SIZE', wolfboot_partition_size)
target_h_data.set('WOLFBOOT_DTS_BOOT_ADDRESS', wolfboot_partition_boot_address)
target_h_data.set('WOLFBOOT_DTS_UPDATE_ADDRESS', wolfboot_partition_update_address)
target_h_data.set('WOLFBOOT_LOAD_ADDRESS', get_option('wolfboot_origin'))
target_h_data.set('WOLFBOOT_LOAD_DTS_ADDRESS', get_option('wolfboot_origin'))
target_h = configure_file(
input: 'include/target.h.in',
output: 'target.h',
configuration: target_h_data,
install_dir: get_option('includedir'),
)
# Generate linker script (unless library target or subproject)
if target != 'library' and not meson.is_subproject()
linker_script_input = 'hal/@0@.ld'.format(target)
if not fs.exists(linker_script_input)
error('Linker script @0@ not found'.format(linker_script_input))
endif
linker_script_data = configuration_data()
linker_script_data.set('ARCH_FLASH_OFFSET', flash_offset)
linker_script_data.set('BOOTLOADER_PARTITION_SIZE', '0x20000') # Default for STM32H7
linker_script_data.set('WOLFBOOT_ORIGIN', get_option('wolfboot_origin'))
linker_script_data.set('WOLFBOOT_PARTITION_BOOT_ADDRESS', wolfboot_partition_boot_address)
linker_script_data.set('WOLFBOOT_PARTITION_UPDATE_ADDRESS', wolfboot_partition_update_address)
linker_script_data.set('WOLFBOOT_PARTITION_SWAP_ADDRESS', wolfboot_partition_swap_address)
linker_script_data.set('WOLFBOOT_PARTITION_SIZE', wolfboot_partition_size)
target_ld = configure_file(
input: linker_script_input,
output: 'target.ld',
configuration: linker_script_data,
)
endif
# Link arguments for executable
link_args = []
if get_option('use_gcc_headless')
link_args += [
'-ffreestanding',
'-nostartfiles',
'-Wl,-gc-sections',
'-Wl,-Map=@0@'.format(meson.current_build_dir() / 'wolfboot.map'),
]
endif
if target != 'library' and not meson.is_subproject()
link_args += ['-T', meson.current_build_dir() / 'target.ld']
endif
# Build tools if requested (include in library mode)
need_keygen = sign != 'NONE' and get_option('flash_otp_keystore') == false
if get_option('build_keytools') or need_keygen
subdir('tools/keytools')
endif
# Build bin-assemble tool - useful for parent projects to assemble firmware images (include in library mode)
if get_option('build_bin_assemble')
subdir('tools/bin-assemble')
endif
# Generate keystore.c (include in library mode for tool availability)
if need_keygen
public_key_path = get_option('public_key_path')
# Convert sign option to keygen flag
keygen_sign_flag = ''
if sign == 'ECC256'
keygen_sign_flag = '--ecc256'
elif sign == 'ECC384'
keygen_sign_flag = '--ecc384'
elif sign == 'ECC521'
keygen_sign_flag = '--ecc521'
elif sign == 'ED25519'
keygen_sign_flag = '--ed25519'
elif sign == 'ED448'
keygen_sign_flag = '--ed448'
elif sign == 'RSA2048'
keygen_sign_flag = '--rsa2048'
elif sign == 'RSA3072'
keygen_sign_flag = '--rsa3072'
elif sign == 'RSA4096'
keygen_sign_flag = '--rsa4096'
elif sign == 'LMS'
keygen_sign_flag = '--lms'
elif sign == 'XMSS'
keygen_sign_flag = '--xmss'
elif sign == 'ML_DSA'
keygen_sign_flag = '--ml-dsa'
endif
if keygen_sign_flag != ''
if public_key_path != ''
keystore_c_file = meson.current_build_dir() / 'keystore.c'
# Generate keystore.c from external public key
# Note: keygen tool writes to src/keystore.c by default, so we move it to build dir
keystore_c_target = custom_target('keystore_c',
input: public_key_path,
output: 'keystore.c',
command: [
'sh', '-c',
'&&'.join([
'cd @0@'.format(meson.current_source_dir()),
'mkdir -p src',
'@0@ --force @1@ -i @2@'.format(keygen_exe.full_path(), keygen_sign_flag, public_key_path),
'mv src/keystore.c @0@'.format(keystore_c_file)])
],
depends: [keygen_exe],
build_by_default: true,
)
# Add generated keystore to sources
public_key_sources += [keystore_c_target]
message('Generated keystore.c from external public key: @0@'.format(public_key_path))
else
# Generate keystore.c from generated private key (standard workflow)
private_key_file = meson.current_build_dir() / 'wolfboot_signing_private_key.der'
keystore_c_file = meson.current_build_dir() / 'keystore.c'
keystore_c_target = custom_target('keystore_c',
output: ['wolfboot_signing_private_key.der', 'keystore.c'],
command: [
'sh', '-c',
'&&'.join([
'cd @0@'.format(meson.current_source_dir()),
'mkdir -p src',
'@0@ --force @1@ -g @2@'.format(keygen_exe.full_path(), keygen_sign_flag, private_key_file),
'mv src/keystore.c @0@'.format(keystore_c_file)
])
],
depends: [keygen_exe],
build_by_default: true,
)
# Add generated keystore to sources
public_key_sources += [keystore_c_target]
message('Generated keystore.c from generated private key for sign=@0@'.format(sign))
endif
else
error('Unsupported signature algorithm for keystore generation: @0@'.format(sign))
endif
endif
# Build public key library after keystore generation (skip in library mode)
if target != 'library'
library_sources = public_key_sources
if library_sources.length() == 0
library_sources = ['src/string.c'] # dummy source if empty
endif
libpublic_key = static_library(
'public_key',
library_sources,
include_directories: inc_dirs,
c_args: c_args,
dependencies: generated_inc, # Include generated headers
install: false,
)
endif
# Build libwolfboot static library (matches CMake libwolfboot)
# In library mode, create simple libwolfboot without HAL dependencies
if target == 'library'
libwolfboot = static_library(
'wolfboot',
['src/libwolfboot.c'], # No flash sources in library mode
include_directories: inc_dirs,
c_args: c_args + ['-Wno-unused'],
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
install: false,
)
else
# Handle conditional linking based on what libraries exist
link_libraries = []
if target != 'library' and not meson.is_subproject()
link_libraries += [libwolfboothal]
endif
if target != 'library'
link_libraries += [libpublic_key]
endif
libwolfboot = static_library(
'wolfboot',
['src/libwolfboot.c'] + flash_sources,
include_directories: inc_dirs,
c_args: c_args + ['-Wno-unused'],
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
link_with: link_libraries,
install: false,
)
endif
# Build main wolfboot implementation library (skip in library mode)
if target != 'library'
libwolfboot_main = static_library(
'wolfboot_main',
wolfboot_sources,
include_directories: inc_dirs,
c_args: c_args,
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
install: false,
)
endif
# Build main wolfboot executable (unless library target or subproject)
if target != 'library' and not meson.is_subproject()
# Build executable link libraries list
exe_link_libraries = []
if target != 'library' and not meson.is_subproject()
exe_link_libraries += [libwolfboothal]
endif
if target != 'library'
exe_link_libraries += [libpublic_key]
endif
wolfboot_elf = executable(
'wolfboot.elf',
[], # No additional sources, everything is in the libraries
include_directories: inc_dirs,
c_args: c_args,
link_args: link_args,
link_whole: [libwolfboot_main],
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
link_with: exe_link_libraries,
install: false,
)
# Create binary from ELF
if meson.is_cross_build()
# When cross-compiling, use the cross-compiler's objcopy
objcopy_prog = find_program('arm-none-eabi-objcopy', required: true)
else
objcopy_prog = find_program(get_option('objcopy_prog'), required: true)
endif
wolfboot_bin = custom_target(
'wolfboot.bin',
input: wolfboot_elf,
output: 'wolfboot.bin',
command: [objcopy_prog, '-O', 'binary', '@INPUT@', '@OUTPUT@'],
install: false,
build_by_default: true,
)
endif
# Summary
summary({
'Architecture': arch,
'Target': target,
'Signature': sign,
'Hash': hash_alg,
'Encryption': encrypt_enabled,
'SP Math All': get_option('spmathall'),
'SP Math': get_option('spmath'),
'Subproject Mode': meson.is_subproject(),
'Library Mode': target == 'library',
}, section: 'Configuration')
# Declare dependencies for use as subproject
# This makes all wolfBoot libraries available to parent projects
wolfboot_dep = declare_dependency(
link_with: libwolfboot,
include_directories: inc_dirs,
compile_args: c_args,
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
)
# Conditional dependencies based on what was built
if target != 'library'
# Build link libraries list for main dependency
main_link_libraries = [libwolfboot_main]
if target != 'library' and not meson.is_subproject()
main_link_libraries += [libwolfboothal]
endif
if target != 'library'
main_link_libraries += [libpublic_key]
endif
wolfboot_main_dep = declare_dependency(
link_with: main_link_libraries,
include_directories: inc_dirs,
compile_args: c_args,
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
)
# Build complete dependency link list
complete_link_libraries = [libwolfboot]
if target != 'library' and not meson.is_subproject()
complete_link_libraries += [libwolfboothal]
endif
if target != 'library'
complete_link_libraries += [libpublic_key]
endif
# Complete wolfboot dependency with all components
wolfboot_complete_dep = declare_dependency(
link_with: complete_link_libraries,
include_directories: inc_dirs,
compile_args: c_args,
dependencies: [wolfcrypt_dep, generated_inc], # Include generated headers
)
else
# In library mode, only provide basic wolfboot dependency
wolfboot_main_dep = wolfboot_dep
wolfboot_complete_dep = wolfboot_dep
endif
# HAL dependency (only if HAL was built)
if target != 'library' and not meson.is_subproject()
wolfboothal_dep = declare_dependency(
link_with: libwolfboothal,
include_directories: inc_dirs,
compile_args: c_args,
dependencies: generated_inc, # Include generated headers
)
endif
# Public key dependency (only if public key library was built)
if target != 'library'
wolfboot_public_key_dep = declare_dependency(
link_with: libpublic_key,
include_directories: inc_dirs,
compile_args: c_args,
dependencies: generated_inc, # Include generated headers
)
endif
# Make libraries available as variables for direct access
meson.override_dependency('wolfboot', wolfboot_dep)
meson.override_dependency('wolfboot-complete', wolfboot_complete_dep)
# Conditional overrides based on what was built
if target != 'library' and not meson.is_subproject()
meson.override_dependency('wolfboothal', wolfboothal_dep)
endif
if target != 'library'
meson.override_dependency('wolfboot-public-key', wolfboot_public_key_dep)
endif
# Also make individual libraries available (conditional)
wolfboot_libraries = {
'libwolfboot': libwolfboot,
}
if target != 'library'
wolfboot_libraries += {
'libwolfboot_main': libwolfboot_main,
'libpublic_key': libpublic_key,
}
endif
if target != 'library' and not meson.is_subproject()
wolfboot_libraries += {
'libwolfboothal': libwolfboothal,
}
endif
# Export important configuration variables for parent projects
wolfboot_config = {
'arch': arch,
'target': target,
'sign': sign,
'hash_alg': hash_alg,
'c_args': c_args,
'inc_dirs': inc_dirs,
'image_signature_size': image_signature_size,
'image_header_size': image_header_size,
'target_h': target_h, # Export the generated target.h for subprojects
}
if meson.is_subproject()
message('wolfBoot configured successfully as subproject - libraries and dependencies exposed, executables skipped')
elif target == 'library'
message('wolfBoot configured successfully in library mode - only libwolfboot static library built')
else
message('wolfBoot configured successfully')
endif