@@ -108,7 +108,7 @@ static int dumpfile_iterate(FAR char *path, dumpfile_cb_t cb, FAR void *arg)
108
108
ret = mkdir (path , 0777 );
109
109
if (ret < 0 )
110
110
{
111
- return ret ;
111
+ return - errno ;
112
112
}
113
113
}
114
114
@@ -155,6 +155,61 @@ static void dumpfile_delete(FAR char *path, FAR const char *filename,
155
155
}
156
156
}
157
157
158
+ /****************************************************************************
159
+ * dumpfile_get_info
160
+ ****************************************************************************/
161
+
162
+ static int dumpfile_get_info (int fd , FAR struct coredump_info_s * info )
163
+ {
164
+ Elf_Ehdr ehdr ;
165
+ Elf_Phdr phdr ;
166
+ Elf_Nhdr nhdr ;
167
+ char name [COREDUMP_INFONAME_SIZE ];
168
+
169
+ if (read (fd , & ehdr , sizeof (ehdr )) != sizeof (ehdr ) ||
170
+ memcmp (ehdr .e_ident , ELFMAG , EI_MAGIC_SIZE ) != 0 )
171
+ {
172
+ return - EINVAL ;
173
+ }
174
+
175
+ /* The last program header is for NuttX core info note. */
176
+
177
+ if (lseek (fd , ehdr .e_phoff + ehdr .e_phentsize * (ehdr .e_phnum - 1 ),
178
+ SEEK_SET ) < 0 )
179
+ {
180
+ return - errno ;
181
+ }
182
+
183
+ if (read (fd , & phdr , sizeof (phdr )) != sizeof (phdr ) ||
184
+ lseek (fd , phdr .p_offset , SEEK_SET ) < 0 )
185
+ {
186
+ return - errno ;
187
+ }
188
+
189
+ /* The note header must match exactly. */
190
+
191
+ if (read (fd , & nhdr , sizeof (nhdr )) != sizeof (nhdr ) ||
192
+ nhdr .n_type != COREDUMP_MAGIC ||
193
+ nhdr .n_namesz != COREDUMP_INFONAME_SIZE ||
194
+ nhdr .n_descsz != sizeof (struct coredump_info_s ))
195
+ {
196
+ return - EINVAL ;
197
+ }
198
+
199
+ if (read (fd , name , nhdr .n_namesz ) != nhdr .n_namesz ||
200
+ strcmp (name , "NuttX" ) != 0 )
201
+ {
202
+ return - EINVAL ;
203
+ }
204
+
205
+ if (read (fd , info , sizeof (* info )) != sizeof (* info ))
206
+ {
207
+ return - errno ;
208
+ }
209
+
210
+ return 0 ;
211
+ }
212
+
158
213
/****************************************************************************
159
214
* coredump_restore
160
215
****************************************************************************/
@@ -172,30 +227,21 @@ static void coredump_restore(FAR char *savepath, size_t maxfile)
172
227
int blkfd ;
173
228
off_t off ;
174
229
int ret ;
230
+ Elf_Nhdr nhdr =
231
+ {
232
+ 0
233
+ };
175
234
176
235
blkfd = open (CONFIG_SYSTEM_COREDUMP_DEVPATH , O_RDWR );
177
236
if (blkfd < 0 )
178
237
{
179
238
return ;
180
239
}
181
240
182
- off = lseek (blkfd , - (off_t )sizeof (info ), SEEK_END );
183
- if (off < 0 )
184
- {
185
- printf ("Seek %s fail\n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
186
- goto blkfd_err ;
187
- }
188
-
189
- readsize = read (blkfd , & info , sizeof (info ));
190
- if (readsize != sizeof (info ))
191
- {
192
- printf ("Read %s fail\n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
193
- goto blkfd_err ;
194
- }
195
-
196
- if (info .magic != COREDUMP_MAGIC )
241
+ ret = dumpfile_get_info (blkfd , & info );
242
+ if (ret < 0 )
197
243
{
198
- printf ("%s coredump not found! \n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
244
+ printf ("No core data in %s \n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
199
245
goto blkfd_err ;
200
246
}
201
247
@@ -277,16 +323,33 @@ static void coredump_restore(FAR char *savepath, size_t maxfile)
277
323
printf ("Coredump finish [%s][%zu]\n" , dumppath , info .size );
278
324
}
279
325
280
- info .magic = 0 ;
281
- off = lseek (blkfd , - (off_t )sizeof (info ), SEEK_END );
326
+ off = info .size - sizeof (info );
327
+ off -= COREDUMP_INFONAME_SIZE ;
328
+ off -= sizeof (Elf_Nhdr );
329
+ off = lseek (blkfd , off , SEEK_SET );
330
+ if (off < 0 )
331
+ {
332
+ printf ("Seek %s fail\n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
333
+ goto swap_err ;
334
+ }
335
+
336
+ writesize = write (blkfd , & nhdr , sizeof (nhdr ));
337
+ if (writesize != sizeof (nhdr ))
338
+ {
339
+ printf ("Write %s fail\n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
340
+ }
341
+
342
+ /* Erase the core file header too */
343
+
344
+ off = lseek (blkfd , 0 , SEEK_SET );
282
345
if (off < 0 )
283
346
{
284
347
printf ("Seek %s fail\n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
285
348
goto swap_err ;
286
349
}
287
350
288
- writesize = write (blkfd , & info , sizeof ( info ) );
289
- if (writesize != sizeof ( info ) )
351
+ writesize = write (blkfd , "\0\0\0\0" , EI_MAGIC_SIZE );
352
+ if (writesize != EI_MAGIC_SIZE )
290
353
{
291
354
printf ("Write %s fail\n" , CONFIG_SYSTEM_COREDUMP_DEVPATH );
292
355
}
0 commit comments