]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/mtd_probe/probe_smartmedia.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2010 - Maxim Levitsky
5 * mtd_probe is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * mtd_probe is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with mtd_probe; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
23 #include <mtd/mtd-user.h>
29 #include <sys/types.h>
32 #include "alloc-util.h"
33 #include "mtd_probe.h"
35 static const uint8_t cis_signature
[] = {
36 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
39 int probe_smart_media(int mtd_fd
, mtd_info_t
* info
) {
44 _cleanup_free_
uint8_t *cis_buffer
= NULL
;
48 cis_buffer
= malloc(SM_SECTOR_SIZE
);
52 if (info
->type
!= MTD_NANDFLASH
)
53 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
54 "Not marked MTD_NANDFLASH.");
56 sector_size
= info
->writesize
;
57 block_size
= info
->erasesize
;
58 size_in_megs
= info
->size
/ (1024 * 1024);
60 if (!IN_SET(sector_size
, SM_SECTOR_SIZE
, SM_SMALL_PAGE
))
61 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
62 "Unexpected sector size: %i", sector_size
);
64 switch(size_in_megs
) {
77 for (offset
= 0; offset
< block_size
* spare_count
; offset
+= sector_size
) {
78 (void) lseek(mtd_fd
, SEEK_SET
, offset
);
80 if (read(mtd_fd
, cis_buffer
, SM_SECTOR_SIZE
) == SM_SECTOR_SIZE
) {
87 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
90 if (memcmp(cis_buffer
, cis_signature
, sizeof(cis_signature
)) != 0 &&
91 memcmp(cis_buffer
+ SM_SMALL_PAGE
, cis_signature
, sizeof(cis_signature
)) != 0)
92 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
93 "CIS signature didn't match");
95 printf("MTD_FTL=smartmedia\n");