@@ -70,14 +70,29 @@ CHIP_ERROR ErasePage(uint32_t addr)
70
70
return chip::DeviceLayer::Silabs::GetPlatform ().FlashErasePage (addr);
71
71
}
72
72
73
- CHIP_ERROR WritePage ( uint32_t addr, const uint8_t * data, size_t size )
73
+ size_t RoundNearest ( size_t n, size_t multiple )
74
74
{
75
- return chip::DeviceLayer::Silabs::GetPlatform (). FlashWritePage (addr, data, size) ;
75
+ return (n % multiple) > 0 ? n + (multiple - n % multiple) : n ;
76
76
}
77
77
78
- size_t RoundNearest (size_t n, size_t multiple)
78
+ /* *
79
+ * Writes "size" bytes to the flash page. The data is padded with 0xff
80
+ * up to the nearest 32-bit boundary. The "max" argument is used to ensure
81
+ * that the padding won't exceed the limits of the buffer.
82
+ */
83
+ CHIP_ERROR WritePage (uint32_t addr, const uint8_t * data, size_t size, size_t max)
79
84
{
80
- return (n % multiple) > 0 ? n + (multiple - n % multiple) : n;
85
+ // The flash driver fails if the size is not a multiple of 4 (32-bits)
86
+ size_t size_32 = RoundNearest (size, 4 );
87
+ // If the input data is smaller than the 32-bit size, pad the buffer with "0xff"
88
+ if (size_32 != size)
89
+ {
90
+ uint8_t * p = (uint8_t *) data;
91
+ VerifyOrReturnError (size_32 <= max, CHIP_ERROR_BUFFER_TOO_SMALL);
92
+ memset (p + size, 0xff , size_32 - size);
93
+ size = size_32;
94
+ }
95
+ return chip::DeviceLayer::Silabs::GetPlatform ().FlashWritePage (addr, data, size);
81
96
}
82
97
83
98
CHIP_ERROR WriteFile (Storage & store, SilabsConfig::Key offset_key, SilabsConfig::Key size_key, const ByteSpan & value)
@@ -89,17 +104,7 @@ CHIP_ERROR WriteFile(Storage & store, SilabsConfig::Key offset_key, SilabsConfig
89
104
ReturnErrorOnFailure (ErasePage (base_addr));
90
105
}
91
106
92
- memcpy (Storage::aux_buffer, value.data (), value.size ());
93
- if (value.size () < Storage::kArgumentSizeMax )
94
- {
95
- memset (Storage::aux_buffer + value.size (), 0xff , Storage::kArgumentSizeMax - value.size ());
96
- }
97
-
98
- ChipLogProgress (DeviceLayer, " WriteFile, addr:0x%06x+%03u, size:%u" , (unsigned ) base_addr, (unsigned ) sCredentialsOffset ,
99
- (unsigned ) value.size ());
100
- // ChipLogByteSpan(DeviceLayer, ByteSpan(value.data(), value.size() < kDebugLength ? value.size() : kDebugLength));
101
-
102
- ReturnErrorOnFailure (WritePage (base_addr + sCredentialsOffset , Storage::aux_buffer, Storage::kArgumentSizeMax ));
107
+ ReturnErrorOnFailure (WritePage (base_addr + sCredentialsOffset , value.data (), value.size (), store.GetBufferSize ()));
103
108
104
109
// Store file offset
105
110
ReturnErrorOnFailure (SilabsConfig::WriteConfigValue (offset_key, (uint32_t ) sCredentialsOffset ));
@@ -119,7 +124,6 @@ CHIP_ERROR ReadFileByOffset(Storage & store, const char * description, uint32_t
119
124
ByteSpan span (address, size);
120
125
ChipLogProgress (DeviceLayer, " %s, addr:0x%06x+%03u, size:%u" , description, (unsigned ) base_addr, (unsigned ) offset,
121
126
(unsigned ) size);
122
- // ChipLogByteSpan(DeviceLayer, ByteSpan(span.data(), span.size() < kDebugLength ? span.size() : kDebugLength));
123
127
return CopySpanToMutableSpan (span, value);
124
128
}
125
129
0 commit comments