xref: /freebsd/usr.sbin/fstyp/befs.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
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 __FBSDID("$FreeBSD$");
320e92585cSPiotr Pawel Stefaniak 
330e92585cSPiotr Pawel Stefaniak #include <stdint.h>
340e92585cSPiotr Pawel Stefaniak #include <stdio.h>
350e92585cSPiotr Pawel Stefaniak #include <stdlib.h>
360e92585cSPiotr Pawel Stefaniak #include <string.h>
370e92585cSPiotr Pawel Stefaniak 
380e92585cSPiotr Pawel Stefaniak #include "fstyp.h"
390e92585cSPiotr Pawel Stefaniak 
400e92585cSPiotr Pawel Stefaniak #define B_OS_NAME_LENGTH	32
410e92585cSPiotr Pawel Stefaniak #define BEFS_BLOCK_OFFSET	512
420e92585cSPiotr Pawel Stefaniak #define BEFS_SUPER_BLOCK_MAGIC1	0x42465331
430e92585cSPiotr Pawel Stefaniak 
440e92585cSPiotr Pawel Stefaniak struct disk_super_block {
450e92585cSPiotr Pawel Stefaniak     char	name[B_OS_NAME_LENGTH];
460e92585cSPiotr Pawel Stefaniak     int32_t	magic1;
470e92585cSPiotr Pawel Stefaniak };
480e92585cSPiotr Pawel Stefaniak 
490e92585cSPiotr Pawel Stefaniak int
500e92585cSPiotr Pawel Stefaniak fstyp_befs(FILE *fp, char *label, size_t size)
510e92585cSPiotr Pawel Stefaniak {
520e92585cSPiotr Pawel Stefaniak 	struct disk_super_block *volume;
530e92585cSPiotr Pawel Stefaniak 
540e92585cSPiotr Pawel Stefaniak 	volume = read_buf(fp, BEFS_BLOCK_OFFSET, sizeof(*volume));
550e92585cSPiotr Pawel Stefaniak 
560e92585cSPiotr Pawel Stefaniak 	if (volume == NULL) {
570e92585cSPiotr Pawel Stefaniak 		return (1);
580e92585cSPiotr Pawel Stefaniak 	}
590e92585cSPiotr Pawel Stefaniak 
600e92585cSPiotr Pawel Stefaniak 	if (volume->magic1 == BEFS_SUPER_BLOCK_MAGIC1) {
610e92585cSPiotr Pawel Stefaniak 		strlcpy(label, volume->name, size);
620e92585cSPiotr Pawel Stefaniak 		free(volume);
630e92585cSPiotr Pawel Stefaniak 
640e92585cSPiotr Pawel Stefaniak 		return (0);
650e92585cSPiotr Pawel Stefaniak 	}
660e92585cSPiotr Pawel Stefaniak 
670e92585cSPiotr Pawel Stefaniak 	free(volume);
680e92585cSPiotr Pawel Stefaniak 
690e92585cSPiotr Pawel Stefaniak 	return (1);
700e92585cSPiotr Pawel Stefaniak }
71