b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | --- a/lib/libubigen.c |
| 2 | +++ b/lib/libubigen.c |
| 3 | @@ -122,8 +122,9 @@ int ubigen_add_volume(const struct ubige |
| 4 | return 0; |
| 5 | } |
| 6 | |
| 7 | -void ubigen_init_ec_hdr(const struct ubigen_info *ui, |
| 8 | - struct ubi_ec_hdr *hdr, long long ec) |
| 9 | +static void __ubigen_init_ec_hdr(const struct ubigen_info *ui, |
| 10 | + struct ubi_ec_hdr *hdr, long long ec, |
| 11 | + int eof) |
| 12 | { |
| 13 | uint32_t crc; |
| 14 | |
| 15 | @@ -136,10 +137,22 @@ void ubigen_init_ec_hdr(const struct ubi |
| 16 | hdr->data_offset = cpu_to_be32(ui->data_offs); |
| 17 | hdr->image_seq = cpu_to_be32(ui->image_seq); |
| 18 | |
| 19 | + if (eof) { |
| 20 | + hdr->padding1[0] = 'E'; |
| 21 | + hdr->padding1[1] = 'O'; |
| 22 | + hdr->padding1[2] = 'F'; |
| 23 | + } |
| 24 | + |
| 25 | crc = mtd_crc32(UBI_CRC32_INIT, hdr, UBI_EC_HDR_SIZE_CRC); |
| 26 | hdr->hdr_crc = cpu_to_be32(crc); |
| 27 | } |
| 28 | |
| 29 | +void ubigen_init_ec_hdr(const struct ubigen_info *ui, |
| 30 | + struct ubi_ec_hdr *hdr, long long ec) |
| 31 | +{ |
| 32 | + __ubigen_init_ec_hdr(ui, hdr, ec, 0); |
| 33 | +} |
| 34 | + |
| 35 | void ubigen_init_vid_hdr(const struct ubigen_info *ui, |
| 36 | const struct ubigen_vol_info *vi, |
| 37 | struct ubi_vid_hdr *hdr, int lnum, |
| 38 | @@ -307,6 +320,39 @@ int ubigen_write_layout_vol(const struct |
| 39 | } |
| 40 | |
| 41 | free(outbuf); |
| 42 | + return 0; |
| 43 | + |
| 44 | +out_free: |
| 45 | + free(outbuf); |
| 46 | + return -1; |
| 47 | +} |
| 48 | + |
| 49 | +int ubigen_write_eof_markers(const struct ubigen_info *ui, long long ec, |
| 50 | + int count, int out_fd) |
| 51 | +{ |
| 52 | + char *outbuf; |
| 53 | + int peb_size = ui->peb_size; |
| 54 | + |
| 55 | + outbuf = malloc(peb_size); |
| 56 | + if (!outbuf) { |
| 57 | + sys_errmsg("cannot allocate %d bytes of memory", peb_size); |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + memset(outbuf, 0xFF, peb_size); |
| 62 | + __ubigen_init_ec_hdr(ui, (struct ubi_ec_hdr *)outbuf, ec, 1); |
| 63 | + |
| 64 | + while (count) { |
| 65 | + if (write(out_fd, outbuf, peb_size) != peb_size) { |
| 66 | + sys_errmsg("cannot write %d bytes to the output file", |
| 67 | + peb_size); |
| 68 | + goto out_free; |
| 69 | + } |
| 70 | + |
| 71 | + count--; |
| 72 | + } |
| 73 | + |
| 74 | + free(outbuf); |
| 75 | return 0; |
| 76 | |
| 77 | out_free: |
| 78 | --- a/include/libubigen.h |
| 79 | +++ b/include/libubigen.h |
| 80 | @@ -187,6 +187,9 @@ int ubigen_write_layout_vol(const struct |
| 81 | long long ec1, long long ec2, |
| 82 | struct ubi_vtbl_record *vtbl, int fd); |
| 83 | |
| 84 | +int ubigen_write_eof_markers(const struct ubigen_info *ui, long long ec, |
| 85 | + int count, int out_fd); |
| 86 | + |
| 87 | #ifdef __cplusplus |
| 88 | } |
| 89 | #endif |