/* * GRUB -- GRand Unified Bootloader * Copyright (C) 2006 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ /* From Solaris usr/src/stand/lib/fs/ufs/ufsops.c */ #include #include "ufs.h" /* These are the pools of buffers, etc. */ #define SUPERBLOCK ((struct fs *)(FSYS_BUF + 0x2000)) #define INODE ((struct icommon *)(FSYS_BUF + 0x1000)) #define DIRENT (FSYS_BUF + 0x4000) #define MAXBSIZE ((FSYS_BUFLEN - 0x4000) / 2) #define INDIRBLK1 ((grub_daddr32_t *)(FSYS_BUF + 0x4000)) /* 2+ indir blk */ #define INDIRBLK0 ((grub_daddr32_t *)(FSYS_BUF+ 0x4000 + MAXBSIZE)) /* 1st indirect blk */ #define indirblk0 (*fsig_int1(ffi)) #define indirblk1 (*fsig_int2(ffi)) static int openi(fsi_file_t *, grub_ino_t); static grub_ino_t dlook(fsi_file_t *, grub_ino_t, char *); static grub_daddr32_t sbmap(fsi_file_t *, grub_daddr32_t); /* read superblock and check fs magic */ static int ufs_mount(fsi_file_t *ffi, const char *options) { if (/*! IS_PC_SLICE_TYPE_SOLARIS(current_slice) || */ !devread(ffi, UFS_SBLOCK, 0, UFS_SBSIZE, (char *)SUPERBLOCK) || SUPERBLOCK->fs_magic != UFS_MAGIC || MAXBSIZE < SUPERBLOCK->fs_bsize) return 0; return 1; } /* * searching for a file, if successful, inode will be loaded in INODE * The entry point should really be named ufs_open(char *pathname). * For now, keep it consistent with the rest of fsys modules. */ static int ufs_dir(fsi_file_t *ffi, char *dirname) { grub_ino_t inode = ROOTINO; /* start from root */ char *fname, ch; indirblk0 = indirblk1 = 0; /* skip leading slashes */ while (*dirname == '/') dirname++; while (inode && *dirname && !isspace((uint8_t)*dirname)) { if (!openi(ffi, inode)) return 0; /* parse for next path component */ fname = dirname; while (*dirname && !isspace((uint8_t)*dirname) && *dirname != '/') dirname++; ch = *dirname; *dirname = 0; /* ensure null termination */ inode = dlook(ffi, inode, fname); *dirname = ch; while (*dirname == '/') dirname++; } /* return 1 only if inode exists and is a regular file */ if (! openi(ffi, inode)) return (0); filepos = 0; filemax = INODE->ic_sizelo; return (inode && ((INODE->ic_smode & IFMT) == IFREG)); } /* * This is the high-level read function. */ static int ufs_read(fsi_file_t *ffi, char *buf, int len) { int off, size, ret = 0, ok; grub_daddr32_t lblk, dblk; while (len) { off = blkoff(SUPERBLOCK, filepos); lblk = lblkno(SUPERBLOCK, filepos); size = SUPERBLOCK->fs_bsize; size -= off; if (size > len) size = len; if ((dblk = sbmap(ffi, lblk)) <= 0) { /* we are in a file hole, just zero the buf */ grub_memset(buf, 0, size); } else { disk_read_func = disk_read_hook; ok = devread(ffi, fsbtodb(SUPERBLOCK, dblk), off, size, buf); disk_read_func = 0; if (!ok) return 0; } buf += size; len -= size; filepos += size; ret += size; } return (ret); } int ufs_embed (int *start_sector, int needed_sectors) { if (needed_sectors > 14) return 0; *start_sector = 2; return 1; } /* read inode and place content in INODE */ static int openi(fsi_file_t *ffi, grub_ino_t inode) { grub_daddr32_t dblk; int off; /* get block and byte offset into the block */ dblk = fsbtodb(SUPERBLOCK, itod(SUPERBLOCK, inode)); off = itoo(SUPERBLOCK, inode) * sizeof (struct icommon); return (devread(ffi, dblk, off, sizeof (struct icommon), (char *)INODE)); } /* * Performs fileblock mapping. Convert file block no. to disk block no. * Returns 0 when block doesn't exist and <0 when block isn't initialized * (i.e belongs to a hole in the file). */ grub_daddr32_t sbmap(fsi_file_t *ffi, grub_daddr32_t bn) { int level, bound, i, index; grub_daddr32_t nb, blkno; grub_daddr32_t *db = INODE->ic_db; /* blocks 0..UFS_NDADDR are direct blocks */ if (bn < UFS_NDADDR) { return db[bn]; } /* determine how many levels of indirection. */ level = 0; bn -= UFS_NDADDR; bound = UFS_NINDIR(SUPERBLOCK); while (bn >= bound) { level++; bn -= bound; bound *= UFS_NINDIR(SUPERBLOCK); } if (level >= UFS_NIADDR) /* bn too big */ return ((grub_daddr32_t)0); /* fetch the first indirect block */ nb = INODE->ic_ib[level]; if (nb == 0) { return ((grub_daddr32_t)0); } if (indirblk0 != nb) { indirblk0 = 0; blkno = fsbtodb(SUPERBLOCK, nb); if (!devread(ffi, blkno, 0, SUPERBLOCK->fs_bsize, (char *)INDIRBLK0)) return (0); indirblk0 = nb; } bound /= UFS_NINDIR(SUPERBLOCK); index = (bn / bound) % UFS_NINDIR(SUPERBLOCK); nb = INDIRBLK0[index]; /* fetch through the indirect blocks */ for (i = 1; i <= level; i++) { if (indirblk1 != nb) { blkno = fsbtodb(SUPERBLOCK, nb); if (!devread(ffi, blkno, 0, SUPERBLOCK->fs_bsize, (char *)INDIRBLK1)) return (0); indirblk1 = nb; } bound /= UFS_NINDIR(SUPERBLOCK); index = (bn / bound) % UFS_NINDIR(SUPERBLOCK); nb = INDIRBLK1[index]; if (nb == 0) return ((grub_daddr32_t)0); } return (nb); } /* search directory content for name, return inode number */ static grub_ino_t dlook(fsi_file_t *ffi, grub_ino_t dir_ino, char *name) { int loc, off; grub_daddr32_t lbn, dbn, dblk; struct direct *dp; if ((INODE->ic_smode & IFMT) != IFDIR) return 0; loc = 0; while (loc < INODE->ic_sizelo) { /* offset into block */ off = blkoff(SUPERBLOCK, loc); if (off == 0) { /* need to read in a new block */ /* get logical block number */ lbn = lblkno(SUPERBLOCK, loc); /* resolve indrect blocks */ dbn = sbmap(ffi, lbn); if (dbn == 0) return (0); dblk = fsbtodb(SUPERBLOCK, dbn); if (!devread(ffi, dblk, 0, SUPERBLOCK->fs_bsize, (char *)DIRENT)) { return 0; } } dp = (struct direct *)(DIRENT + off); if (dp->d_ino && substring(name, dp->d_name) == 0) return (dp->d_ino)
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Header file for VirtualSerialHost.c.
 */

#ifndef _VIRTUALSERIAL_HOST_H_
#define _VIRTUALSERIAL_HOST_H_

	/* Includes: */
		#include <avr/io.h>
		#include <avr/wdt.h>
		#include <avr/pgmspace.h>
		#include <avr/power.h>
		#include <avr/interrupt.h>
		#include <stdio.h>

		#include <LUFA/Drivers/Misc/TerminalCodes.h>
		#include <LUFA/Drivers/Peripheral/Serial.h>
		#include <LUFA/Drivers/Board/LEDs.h>
		#include <LUFA/Drivers/USB/USB.h>
		#include <LUFA/Platform/Platform.h>

	/* Macros: */
		/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
		#define LEDMASK_USB_NOTREADY      LEDS_LED1

		/** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
		#define LEDMASK_USB_ENUMERATING  (LEDS_LED2 | LEDS_LED3)

		/** LED mask for the library LED driver, to indicate that the USB interface is ready. */
		#define LEDMASK_USB_READY        (LEDS_LED2 | LEDS_LED4)

		/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
		#define LEDMASK_USB_ERROR        (LEDS_LED1 | LEDS_LED3)

	/* Function Prototypes: */
		void SetupHardware(void);
		void CDCHost_Task(void);

		void EVENT_USB_Host_HostError(const uint8_t ErrorCode);
		void EVENT_USB_Host_DeviceAttached(void);
		void EVENT_USB_Host_DeviceUnattached(void);
		void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
		                                            const uint8_t SubErrorCode);
		void EVENT_USB_Host_DeviceEnumerationComplete(void);

#endif