xref: /aosp_15_r20/external/mtools/device.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 2021 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  */
17*d5c9a868SElliott Hughes 
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "llong.h"
20*d5c9a868SElliott Hughes #include "device.h"
21*d5c9a868SElliott Hughes 
check_if_sectors_fit(uint32_t tot_sectors,mt_off_t maxBytes,uint32_t sectorSize,char * errmsg)22*d5c9a868SElliott Hughes int check_if_sectors_fit(uint32_t tot_sectors,
23*d5c9a868SElliott Hughes 			 mt_off_t maxBytes,
24*d5c9a868SElliott Hughes 			 uint32_t sectorSize,
25*d5c9a868SElliott Hughes 			 char *errmsg)
26*d5c9a868SElliott Hughes {
27*d5c9a868SElliott Hughes 	if(!maxBytes)
28*d5c9a868SElliott Hughes 		return 0; /* Maxbytes = 0 => no checking */
29*d5c9a868SElliott Hughes 	if(tot_sectors > (smt_off_t) maxBytes / (smt_off_t) sectorSize) {
30*d5c9a868SElliott Hughes 		sprintf(errmsg,
31*d5c9a868SElliott Hughes 			"%d sectors too large for this platform\n",
32*d5c9a868SElliott Hughes 			tot_sectors);
33*d5c9a868SElliott Hughes 		return -1;
34*d5c9a868SElliott Hughes 	}
35*d5c9a868SElliott Hughes 	return 0;
36*d5c9a868SElliott Hughes }
37*d5c9a868SElliott Hughes 
38*d5c9a868SElliott Hughes /*
39*d5c9a868SElliott Hughes  * Calculate number of total sectors on device if needed, and check that
40*d5c9a868SElliott Hughes  * they fit into
41*d5c9a868SElliott Hughes  */
chs_to_totsectors(struct device * dev,char * errmsg)42*d5c9a868SElliott Hughes int chs_to_totsectors(struct device *dev, char *errmsg)
43*d5c9a868SElliott Hughes {
44*d5c9a868SElliott Hughes 	uint32_t sect_per_track, tot_sectors;
45*d5c9a868SElliott Hughes 
46*d5c9a868SElliott Hughes 	if(dev->tot_sectors)
47*d5c9a868SElliott Hughes 		return 0;
48*d5c9a868SElliott Hughes 
49*d5c9a868SElliott Hughes 	if(!dev->heads || !dev->sectors || !dev->tracks)
50*d5c9a868SElliott Hughes 		return 0; /* not fully specified => we cannot do
51*d5c9a868SElliott Hughes 			     anything anyways */
52*d5c9a868SElliott Hughes 
53*d5c9a868SElliott Hughes 	/* Cannot overflow as both dev->heads and dev->sectors are 16
54*d5c9a868SElliott Hughes 	 * bit quantities, whose product will be put into a 32 bit
55*d5c9a868SElliott Hughes 	 * field */
56*d5c9a868SElliott Hughes 	sect_per_track = dev->heads * dev->sectors;
57*d5c9a868SElliott Hughes 
58*d5c9a868SElliott Hughes 	if(dev->tracks > UINT32_MAX / sect_per_track) {
59*d5c9a868SElliott Hughes 		/* Would not fit in 32 bits */
60*d5c9a868SElliott Hughes 
61*d5c9a868SElliott Hughes 		if(errmsg)
62*d5c9a868SElliott Hughes 			sprintf(errmsg,
63*d5c9a868SElliott Hughes 				"Number of sectors larger than 2^32\n");
64*d5c9a868SElliott Hughes 		return -1;
65*d5c9a868SElliott Hughes 	}
66*d5c9a868SElliott Hughes 
67*d5c9a868SElliott Hughes 	tot_sectors = dev->tracks * sect_per_track;
68*d5c9a868SElliott Hughes 	if(tot_sectors > dev->hidden % sect_per_track)
69*d5c9a868SElliott Hughes 		tot_sectors -= dev->hidden % sect_per_track;
70*d5c9a868SElliott Hughes 	dev->tot_sectors = tot_sectors;
71*d5c9a868SElliott Hughes 	return 0;
72*d5c9a868SElliott Hughes }
73