@@ -115,42 +115,88 @@ struct ext2_dir_entry
115115static int write_part (int fd , uint64_t sector , void * buf , size_t size )
116116{
117117 int64_t offset = sector * 512 ;
118- if (iomanX_lseek (fd , offset , SEEK_SET ) != offset ) {
119- printf ("Seek error at sector %ld\n" , (long )sector );
120- return -1 ;
118+ if (iomanX_lseek (fd , offset , SEEK_SET ) != offset ) return -1 ;
119+ if (iomanX_write (fd , buf , size ) != (int )size ) return -1 ;
120+ return 0 ;
121+ }
122+
123+
124+ // Helper: Calculate usable size from APA partition using IOCTLs
125+ // Возвращает количество секторов (512 байт) полезного объема
126+ static uint32_t get_apa_partition_size (int fd ) {
127+ int32_t num_subs ;
128+ uint32_t i ;
129+ uint32_t total_usable_sectors = 0 ;
130+
131+ // 1. Узнаем количество суб-разделов
132+ num_subs = iomanX_ioctl2 (fd , HIOCNSUB , NULL , 0 , NULL , 0 );
133+ if (num_subs < 0 ) {
134+ printf ("MKEXT2: IOCTL HIOCNSUB failed (err: %d)\n" , num_subs );
135+ return 0 ;
121136 }
122- if (iomanX_write (fd , buf , size ) != (int )size ) {
123- printf ("Write error at sector %ld\n" , (long )sector );
124- return -1 ;
137+
138+ printf ("MKEXT2: Found %d sub-partitions.\n" , num_subs );
139+
140+ // 2. Проходим по главному разделу (0) и всем суб-разделам (1..num_subs)
141+ for (i = 0 ; i < (uint32_t )num_subs + 1 ; i ++ ) {
142+ uint32_t raw_size = 0 ;
143+ uint32_t reserved = 0 ;
144+
145+ // Получаем размер текущего куска (в секторах)
146+ int res = iomanX_ioctl2 (fd , HIOCGETSIZE , & i , sizeof (i ), NULL , 0 );
147+ if (res < 0 ) {
148+ printf ("MKEXT2: IOCTL HIOCGETSIZE failed for part %d\n" , i );
149+ return 0 ;
150+ }
151+ raw_size = (uint32_t )res ;
152+
153+ // Определяем оверхед (зарезервированное место)
154+ if (i == 0 ) {
155+ // Main partition: 4 MB reserved (0x2000 sectors)
156+ reserved = 0x2000 ;
157+ } else {
158+ // Sub partition: 2 KB reserved (4 sectors)
159+ reserved = 4 ;
160+ }
161+
162+ if (raw_size > reserved ) {
163+ total_usable_sectors += (raw_size - reserved );
164+ } else {
165+ printf ("MKEXT2: Warning - Part %d size (%u) is smaller than reserved (%u)\n" , i , raw_size , reserved );
166+ }
125167 }
126- return 0 ;
168+
169+ return total_usable_sectors ;
127170}
128171
172+ // MAIN FORMAT FUNCTION
173+ // Теперь аргумент size_mb не нужен, мы определяем его сами!
129174int format_ext2_partition (const char * mount_point )
130175{
131176 char path [256 ];
132177 sprintf (path , "hdd0:%s" , mount_point );
133178
134- printf ("Opening partition %s for EXT2 formatting...\n" , path );
179+ printf ("MKEXT2: Opening %s for auto-sizing and formatting...\n" , path );
135180
136181 int fd = iomanX_open (path , FIO_O_RDWR , 0666 );
137182 if (fd < 0 ) {
138- printf ("Error: Could not open partition %s (err: %d)\n" , path , fd );
183+ printf ("MKEXT2: Error opening partition %s (err: %d)\n" , path , fd );
139184 return -1 ;
140185 }
141186
142- int64_t size_bytes = iomanX_lseek (fd , 0 , SEEK_END );
143- if (size_bytes <= 0 ) {
144- printf ("Error: Could not determine partition size\n" );
187+ // --- AUTO DETECT SIZE ---
188+ uint32_t partition_size_sectors = get_apa_partition_size (fd );
189+
190+ if (partition_size_sectors == 0 ) {
191+ printf ("MKEXT2: Failed to detect usable partition size.\n" );
145192 iomanX_close (fd );
146193 return -1 ;
147194 }
148- iomanX_lseek (fd , 0 , SEEK_SET );
149195
150- uint32_t partition_size_sectors = (uint32_t )(size_bytes / 512 );
151-
152- printf ("Partition size: %u sectors. Formatting...\n" , partition_size_sectors );
196+ printf ("MKEXT2: Total usable size: %u sectors (%.2f MB)\n" ,
197+ partition_size_sectors , (float )partition_size_sectors * 512.0 / 1024.0 / 1024.0 );
153198
199+ // --- EXT2 PARAMETERS ---
154200 uint32_t block_size = 4096 ;
155201 uint32_t sector_per_block = block_size / 512 ;
156202 uint32_t blocks_count = partition_size_sectors / sector_per_block ;
@@ -159,6 +205,8 @@ int format_ext2_partition(const char *mount_point)
159205
160206 uint32_t groups_count = (blocks_count + blocks_per_group - 1 ) / blocks_per_group ;
161207
208+ printf ("MKEXT2: Blocks: %u, Groups: %u\n" , blocks_count , groups_count );
209+
162210 struct ext2_super_block sb ;
163211 memset (& sb , 0 , sizeof (sb ));
164212
0 commit comments