8
8
#if defined(ARDUINO_PORTENTA_C33)
9
9
#include " C33FlashFormatter.h"
10
10
#define BD_ERROR_OK 0
11
+ #include " certificates.h"
11
12
12
13
C33FlashFormatter::C33FlashFormatter ():
13
14
_root(BlockDevice::get_default_instance()),
@@ -24,14 +25,11 @@ bool C33FlashFormatter::checkPartition()
24
25
return false ;
25
26
}
26
27
27
- if (_sys_bd. init () != BD_ERROR_OK || _sys_fs. mount (&_sys_bd) != FR_OK )
28
+ if (! checkCACertificatesPartition () )
28
29
{
29
30
return false ;
30
31
}
31
32
32
- _sys_fs.unmount ();
33
- _sys_bd.deinit ();
34
-
35
33
if (_user_bd.init () != BD_ERROR_OK)
36
34
{
37
35
return false ;
@@ -55,14 +53,13 @@ bool C33FlashFormatter::formatPartition() {
55
53
MBRBlockDevice::partition (_root, 2 , 0x0B , 5 * 1024 * 1024 , 15 * 1024 * 1024 );
56
54
MBRBlockDevice::partition (_root, 3 , 0x0B , 15 * 1024 * 1024 , 16 * 1024 * 1024 );
57
55
58
- int err = _sys_fs. reformat (&_sys_bd);
59
- if (err) {
56
+ if (! flashCACertificates ())
57
+ {
60
58
return false ;
61
59
}
62
60
63
- _sys_fs.unmount ();
64
61
_user_data_fs = new LittleFileSystem (" user" );
65
- err = _user_data_fs->reformat (&_user_bd);
62
+ int err = _user_data_fs->reformat (&_user_bd);
66
63
if (err) {
67
64
return false ;
68
65
}
@@ -71,4 +68,60 @@ bool C33FlashFormatter::formatPartition() {
71
68
return true ;
72
69
}
73
70
71
+ bool C33FlashFormatter::checkCACertificatesPartition ()
72
+ {
73
+ /* Inspired by the CertificateUploader.ino example for Portenta C33*/
74
+ if (_sys_bd.init () != BD_ERROR_OK || _sys_fs.mount (&_sys_bd) != FR_OK)
75
+ {
76
+ return false ;
77
+ }
78
+
79
+ DIR *dir;
80
+ struct dirent *ent;
81
+
82
+ if ((dir = opendir (" /sys" )) == NULL ) {
83
+ return false ;
84
+ }
85
+
86
+ bool foundCert = false ;
87
+ while ((ent = readdir (dir)) != NULL ) {
88
+ String fullname = " /sys/" + String (ent->d_name );
89
+ if (fullname == " /sys/cacert.pem" ) {
90
+ foundCert = true ;
91
+ break ;
92
+ }
93
+ }
94
+ closedir (dir);
95
+
96
+ _sys_fs.unmount ();
97
+ _sys_bd.deinit ();
98
+ return foundCert;
99
+ }
100
+
101
+ bool C33FlashFormatter::flashCACertificates ()
102
+ {
103
+ int err = _sys_fs.reformat (&_sys_bd);
104
+ if (err) {
105
+ return false ;
106
+ }
107
+
108
+ int chunck_size = 128 ;
109
+ int byte_count = 0 ;
110
+ FILE* fp = fopen (" /sys/cacert.pem" , " wb" );
111
+ while (byte_count < cacert_pem_len) {
112
+ if (byte_count + chunck_size > cacert_pem_len)
113
+ chunck_size = cacert_pem_len - byte_count;
114
+ int ret = fwrite (&cacert_pem[byte_count], chunck_size, 1 ,fp);
115
+ if (ret != 1 ) {
116
+ return false ;
117
+ }
118
+ byte_count += chunck_size;
119
+ }
120
+ fclose (fp);
121
+
122
+ _sys_fs.unmount ();
123
+
124
+ return true ;
125
+ }
126
+
74
127
#endif
0 commit comments