xref: /freebsd/usr.sbin/fstyp/befs.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
10e92585cSPiotr Pawel Stefaniak /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
30e92585cSPiotr Pawel Stefaniak  *
40e92585cSPiotr Pawel Stefaniak  * Copyright (c) 2021 Miguel Gocobachi
50e92585cSPiotr Pawel Stefaniak  *
60e92585cSPiotr Pawel Stefaniak  * Redistribution and use in source and binary forms, with or without
70e92585cSPiotr Pawel Stefaniak  * modification, are permitted provided that the following conditions
80e92585cSPiotr Pawel Stefaniak  * are met:
90e92585cSPiotr Pawel Stefaniak  *
100e92585cSPiotr Pawel Stefaniak  * 1. Redistributions of source code must retain the above copyright
110e92585cSPiotr Pawel Stefaniak  *    notice, this list of conditions and the following disclaimer.
120e92585cSPiotr Pawel Stefaniak  * 2. Redistributions in binary form must reproduce the above copyright
130e92585cSPiotr Pawel Stefaniak  *    notice, this list of conditions and the following disclaimer in the
140e92585cSPiotr Pawel Stefaniak  *    documentation and/or other materials provided with the distribution.
150e92585cSPiotr Pawel Stefaniak  *
160e92585cSPiotr Pawel Stefaniak  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170e92585cSPiotr Pawel Stefaniak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180e92585cSPiotr Pawel Stefaniak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190e92585cSPiotr Pawel Stefaniak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200e92585cSPiotr Pawel Stefaniak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210e92585cSPiotr Pawel Stefaniak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220e92585cSPiotr Pawel Stefaniak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230e92585cSPiotr Pawel Stefaniak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240e92585cSPiotr Pawel Stefaniak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250e92585cSPiotr Pawel Stefaniak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260e92585cSPiotr Pawel Stefaniak  * SUCH DAMAGE.
270e92585cSPiotr Pawel Stefaniak  *
280e92585cSPiotr Pawel Stefaniak  */
290e92585cSPiotr Pawel Stefaniak 
300e92585cSPiotr Pawel Stefaniak #include <sys/cdefs.h>
310e92585cSPiotr Pawel Stefaniak #include <stdint.h>
320e92585cSPiotr Pawel Stefaniak #include <stdio.h>
330e92585cSPiotr Pawel Stefaniak #include <stdlib.h>
340e92585cSPiotr Pawel Stefaniak #include <string.h>
350e92585cSPiotr Pawel Stefaniak 
360e92585cSPiotr Pawel Stefaniak #include "fstyp.h"
370e92585cSPiotr Pawel Stefaniak 
380e92585cSPiotr Pawel Stefaniak #define B_OS_NAME_LENGTH	32
390e92585cSPiotr Pawel Stefaniak #define BEFS_BLOCK_OFFSET	512
400e92585cSPiotr Pawel Stefaniak #define BEFS_SUPER_BLOCK_MAGIC1	0x42465331
410e92585cSPiotr Pawel Stefaniak 
420e92585cSPiotr Pawel Stefaniak struct disk_super_block {
430e92585cSPiotr Pawel Stefaniak     char	name[B_OS_NAME_LENGTH];
440e92585cSPiotr Pawel Stefaniak     int32_t	magic1;
450e92585cSPiotr Pawel Stefaniak };
460e92585cSPiotr Pawel Stefaniak 
470e92585cSPiotr Pawel Stefaniak int
fstyp_befs(FILE * fp,char * label,size_t size)480e92585cSPiotr Pawel Stefaniak fstyp_befs(FILE *fp, char *label, size_t size)
490e92585cSPiotr Pawel Stefaniak {
500e92585cSPiotr Pawel Stefaniak 	struct disk_super_block *volume;
510e92585cSPiotr Pawel Stefaniak 
520e92585cSPiotr Pawel Stefaniak 	volume = read_buf(fp, BEFS_BLOCK_OFFSET, sizeof(*volume));
530e92585cSPiotr Pawel Stefaniak 
540e92585cSPiotr Pawel Stefaniak 	if (volume == NULL) {
550e92585cSPiotr Pawel Stefaniak 		return (1);
560e92585cSPiotr Pawel Stefaniak 	}
570e92585cSPiotr Pawel Stefaniak 
580e92585cSPiotr Pawel Stefaniak 	if (volume->magic1 == BEFS_SUPER_BLOCK_MAGIC1) {
590e92585cSPiotr Pawel Stefaniak 		strlcpy(label, volume->name, size);
600e92585cSPiotr Pawel Stefaniak 		free(volume);
610e92585cSPiotr Pawel Stefaniak 
620e92585cSPiotr Pawel Stefaniak 		return (0);
630e92585cSPiotr Pawel Stefaniak 	}
640e92585cSPiotr Pawel Stefaniak 
650e92585cSPiotr Pawel Stefaniak 	free(volume);
660e92585cSPiotr Pawel Stefaniak 
670e92585cSPiotr Pawel Stefaniak 	return (1);
680e92585cSPiotr Pawel Stefaniak }
69