blob: 19603d9641109756047a59322af94ea94fe8318e [file] [log] [blame]
b.liuced8dd02024-06-28 13:28:29 +08001#include "sparse_file.h"
2
3/*---------------------------------------------------------------------------*/
4int
5sparse_file_new(sparse_file_t *sparse_file, size_t offset,
6 size_t size, size_t file_size)
7{
8 sparse_header_t *sparse;
9 chunk_header_t *chunk;
10 unsigned chunk_num = 0;
11
12 /* fill the sparse file structure */
13 sparse = (sparse_header_t *)sparse_file->header;
14 sparse->magic = SPARSE_HEADER_MAGIC;
15 sparse->major_version = 1;
16 sparse->minor_version = 0;
17 sparse->file_hdr_sz = sizeof(sparse_header_t);
18 sparse->chunk_hdr_sz = sizeof(chunk_header_t);
19 sparse->blk_sz = SPARSE_BLOCK_SZ;
20 sparse->total_blks = (file_size + SPARSE_BLOCK_SZ - 1) / SPARSE_BLOCK_SZ;
21 sparse->image_checksum = 0;
22 sparse->total_chunks = 3;
23
24 chunk = (chunk_header_t *)(sparse + 1);
25 chunk->chunk_type = CHUNK_TYPE_DONT_CARE;
26 chunk->reserved1 = 0;
27 chunk->chunk_sz = offset / SPARSE_BLOCK_SZ;
28 chunk->total_sz = sizeof(chunk_header_t);
29 chunk_num += chunk->chunk_sz;
30 chunk++;
31
32 chunk->chunk_type = CHUNK_TYPE_RAW;
33 chunk->reserved1 = 0;
34 chunk->chunk_sz = (size + SPARSE_BLOCK_SZ - 1) / SPARSE_BLOCK_SZ;
35 unsigned fill_size = chunk->chunk_sz * SPARSE_BLOCK_SZ - size;
36 chunk->total_sz = size + fill_size + sizeof(chunk_header_t);
37 chunk_num += chunk->chunk_sz;
38
39 sparse_file->header_size = SPARSE_FILE_HEADER_SIZE;
40 sparse_file->data_size = size;
41 sparse_file->fill_size = fill_size;
42 sparse_file->footer_size = SPARSE_FILE_FOOTER_SIZE;
43
44 chunk = (chunk_header_t *)(sparse_file->footer);
45 chunk->chunk_type = CHUNK_TYPE_DONT_CARE;
46 chunk->reserved1 = 0;
47 chunk->chunk_sz = sparse->total_blks - chunk_num;
48 chunk->total_sz = sizeof(chunk_header_t);
49
50 return 0;
51}
52/*---------------------------------------------------------------------------*/