xref: /netbsd-src/sys/arch/emips/stand/common/raw.c (revision 7991f5a7b8fc83a3d55dc2a1767cca3b84103969)
1*7991f5a7Sandvar /*	$NetBSD: raw.c,v 1.3 2021/07/24 21:31:32 andvar Exp $	*/
25f7e80a8Spooka 
35f7e80a8Spooka /*-
45f7e80a8Spooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
55f7e80a8Spooka  * All rights reserved.
65f7e80a8Spooka  *
75f7e80a8Spooka  * This code was written by Alessandro Forin and Neil Pittman
85f7e80a8Spooka  * at Microsoft Research and contributed to The NetBSD Foundation
95f7e80a8Spooka  * by Microsoft Corporation.
105f7e80a8Spooka  *
115f7e80a8Spooka  * Redistribution and use in source and binary forms, with or without
125f7e80a8Spooka  * modification, are permitted provided that the following conditions
135f7e80a8Spooka  * are met:
145f7e80a8Spooka  * 1. Redistributions of source code must retain the above copyright
155f7e80a8Spooka  *    notice, this list of conditions and the following disclaimer.
165f7e80a8Spooka  * 2. Redistributions in binary form must reproduce the above copyright
175f7e80a8Spooka  *    notice, this list of conditions and the following disclaimer in the
185f7e80a8Spooka  *    documentation and/or other materials provided with the distribution.
195f7e80a8Spooka  *
205f7e80a8Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
215f7e80a8Spooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
225f7e80a8Spooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
235f7e80a8Spooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
245f7e80a8Spooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
255f7e80a8Spooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
265f7e80a8Spooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
275f7e80a8Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
285f7e80a8Spooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
295f7e80a8Spooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
305f7e80a8Spooka  * POSSIBILITY OF SUCH DAMAGE.
315f7e80a8Spooka  */
325f7e80a8Spooka 
335f7e80a8Spooka #include <lib/libsa/stand.h>
345f7e80a8Spooka #include <lib/libkern/libkern.h>
355f7e80a8Spooka #include <machine/emipsreg.h>
365f7e80a8Spooka 
375f7e80a8Spooka #include <sys/param.h>
385f7e80a8Spooka #include <sys/disklabel.h>
395f7e80a8Spooka #include <sys/endian.h>
405f7e80a8Spooka 
415f7e80a8Spooka #include "common.h"
425f7e80a8Spooka #include "raw.h"
435f7e80a8Spooka #include "ace.h"
445f7e80a8Spooka 
455f7e80a8Spooka /* Used to parse strings of the form "a0/99/badface" all hex
465f7e80a8Spooka  */
475f7e80a8Spooka 
hexnum(char c)485f7e80a8Spooka static inline int hexnum(char c)
495f7e80a8Spooka {
505f7e80a8Spooka     if (c >= '0' && c <= '9')
515f7e80a8Spooka         return c - '0';
525f7e80a8Spooka     if (c >= 'a' && c <= 'f')
535f7e80a8Spooka         return c - 'a';
545f7e80a8Spooka     if (c >= 'A' && c <= 'F')
555f7e80a8Spooka         return c - 'A';
565f7e80a8Spooka     return -1;
575f7e80a8Spooka }
585f7e80a8Spooka 
getxnum(char * s,uint32_t * dest)595f7e80a8Spooka static char *getxnum(char *s, uint32_t *dest)
605f7e80a8Spooka {
615f7e80a8Spooka     uint32_t u = 0;
625f7e80a8Spooka     char c;
635f7e80a8Spooka     int v = -1;
645f7e80a8Spooka 
655f7e80a8Spooka     if ((c = *s++) == '/')
665f7e80a8Spooka         c = *s++;
675f7e80a8Spooka     while (c && c != '/') {
685f7e80a8Spooka         v = hexnum(c);
695f7e80a8Spooka         if (v < 0)
705f7e80a8Spooka             break;
715f7e80a8Spooka         u = (u << 4) + v;
725f7e80a8Spooka         c = *s++;
735f7e80a8Spooka     }
745f7e80a8Spooka     /* did we get any */
755f7e80a8Spooka     if (v < 0)
765f7e80a8Spooka         return NULL;
775f7e80a8Spooka     *dest = u;
785f7e80a8Spooka     return s-1;
795f7e80a8Spooka }
805f7e80a8Spooka 
815f7e80a8Spooka /* rawopen("", ctlr, unit, part);
825f7e80a8Spooka  */
835f7e80a8Spooka int
rawopen(struct open_file * f,...)845f7e80a8Spooka rawopen(struct open_file *f, ...)
855f7e80a8Spooka {
865f7e80a8Spooka 	int ctlr, unit, part;
875f7e80a8Spooka     char *file, *cp;
885f7e80a8Spooka 	uint32_t start_sector, sector_count, load_address;
895f7e80a8Spooka     int er;
905f7e80a8Spooka 	size_t cnt;
915f7e80a8Spooka 	va_list ap;
925f7e80a8Spooka 
935f7e80a8Spooka 	va_start(ap, f);
945f7e80a8Spooka 
955f7e80a8Spooka 	ctlr = va_arg(ap, int);
965f7e80a8Spooka 	unit = va_arg(ap, int);
975f7e80a8Spooka 	part = va_arg(ap, int);
985f7e80a8Spooka     file = va_arg(ap, char *);
995f7e80a8Spooka 	va_end(ap);
1005f7e80a8Spooka 
1015f7e80a8Spooka     /* See if we have that controller */
1025f7e80a8Spooka     er = aceopen(f,ctlr,unit,part);
1035f7e80a8Spooka     if (er != 0)
1045f7e80a8Spooka         return er;
1055f7e80a8Spooka 
1065f7e80a8Spooka     /* The string in FILE must tell us three things.. */
1075f7e80a8Spooka     cp = file;
1085f7e80a8Spooka     cp = getxnum(cp,&start_sector);
1095f7e80a8Spooka     if (cp == NULL) goto Bad;
1105f7e80a8Spooka     cp = getxnum(cp,&sector_count);
1115f7e80a8Spooka     if (cp == NULL) goto Bad;
1125f7e80a8Spooka     cp = getxnum(cp,&load_address);
1135f7e80a8Spooka     if (cp == NULL) goto Bad;
1145f7e80a8Spooka 
1155f7e80a8Spooka     //printf("%s -> %u %u %p\n", file, start_sector, sector_count, load_address);
1165f7e80a8Spooka 
1175f7e80a8Spooka     /* Read them sectors */
1185f7e80a8Spooka     er = acestrategy(f->f_devdata, F_READ, start_sector, DEV_BSIZE * sector_count,
1195f7e80a8Spooka                      (void *) load_address, &cnt);
1205f7e80a8Spooka #ifndef LIBSA_NO_DEV_CLOSE
1215f7e80a8Spooka     /* regardless, close the disk */
1225f7e80a8Spooka     aceclose(f);
1235f7e80a8Spooka #endif
1245f7e80a8Spooka     /* How did it go, are we still alive */
1255f7e80a8Spooka     if (er != 0)
1265f7e80a8Spooka         return er;
1275f7e80a8Spooka 
1285f7e80a8Spooka     /* Ok, say it and do it then. */
1295f7e80a8Spooka     printf("Read %u sectors from sector %u at %x, jumping to it..\n",
1305f7e80a8Spooka            sector_count, start_sector, load_address);
1315f7e80a8Spooka 
1325f7e80a8Spooka     call_kernel(load_address,"raw","",0,NULL);
1335f7e80a8Spooka 
1345f7e80a8Spooka  Bad:
1355f7e80a8Spooka #ifndef LIBSA_NO_DEV_CLOSE
1365f7e80a8Spooka     /* regardless, close it */
1375f7e80a8Spooka     aceclose(f);
1385f7e80a8Spooka #endif
1395f7e80a8Spooka     return (ENXIO);
1405f7e80a8Spooka }
1415f7e80a8Spooka 
1425f7e80a8Spooka #ifndef LIBSA_NO_DEV_CLOSE
1435f7e80a8Spooka int
rawclose(struct open_file * f)1445f7e80a8Spooka rawclose(struct open_file *f)
1455f7e80a8Spooka {
1465f7e80a8Spooka     /* Never gets here */
1475f7e80a8Spooka 	return (0);
1485f7e80a8Spooka }
1495f7e80a8Spooka #endif
1505f7e80a8Spooka 
1515f7e80a8Spooka int
rawstrategy(void * devdata,int rw,daddr_t bn,size_t reqcnt,void * addr,size_t * cnt)1525f7e80a8Spooka rawstrategy(
1535f7e80a8Spooka 	void *devdata,
1545f7e80a8Spooka 	int rw,
1555f7e80a8Spooka 	daddr_t bn,
1565f7e80a8Spooka 	size_t reqcnt,
1575f7e80a8Spooka 	void *addr,
158*7991f5a7Sandvar 	size_t *cnt)	/* out: number of bytes transferred */
1595f7e80a8Spooka {
1605f7e80a8Spooka     /* Never gets here */
1615f7e80a8Spooka     return (EIO);
1625f7e80a8Spooka }
1635f7e80a8Spooka 
164